JSP中Http请求实例

服务端发送请求实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@page import="cn.hutool.json.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>HTTP 服务端发送请求实例</h2>
<%
response.setContentType("application/json;charset=utf8");

JSONObject json = new JSONObject();
json.put("msg", "hello world");

out.print(json);
%>
</body>
</html>

使用postman作为客户端请求结果:

1
2
3
{
"msg": "hello world"
}

客户端发送请求实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>HTTP 客户端发送请求实例</h2>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

out.println("username:" + username);
out.println("password:" + password);
%>
</body>
</html>

请求参数:username=zhouhang&password=123456. 请求结果:

1
username:zhouhang password:123456.
文章目录
  1. 1. 服务端发送请求实例:
  2. 2. 使用postman作为客户端请求结果:
  3. 3. 客户端发送请求实例:
  4. 4. 请求参数:username=zhouhang&password=123456. 请求结果: