HttpServletRequest
Tomcat会自动将客户端请求报文封装为HttpServletRequest对象。
HttpServletRequest中请求行常用方法
方法 | 描述 |
---|---|
String getMethod() | 获取请求方法 |
String getProtocol() | 获取请求协议及版本号 |
String getRequestURI() | 获取请求的具体资源 |
StringBuffer getRequestURL() | 获取请求的URL |
请求行常用方法栗子
TestServlet4
@WebServlet("/Test4")
public class TestServlet4 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//请求行相关的栗子
System.out.println("请求方法:"+req.getMethod());
System.out.println("请求协议及版本号:"+req.getProtocol());
//URI是客户端请求的某具体资源的路径,URL是完整路径(包括域名、端口)
System.out.println("URI:"+req.getRequestURI());
System.out.println("URL:"+req.getRequestURL());
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test4
//控制台输出
请求方法:GET
请求协议及版本号:HTTP/1.1
URI:/JavaWeb_war_exploded/Test4
URL:http://localhost:8080/JavaWeb_war_exploded/Test4
更换表单,使用POST的请求方式请求资源。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/Test4">
<input type="submit" value="提交">
</form>
</body>
</html>
//控制台输出
请求方法:POST
请求协议及版本号:HTTP/1.1
URI:/JavaWeb_war_exploded/Test4
URL:http://localhost:8080/JavaWeb_war_exploded/Test4
HttpServletRequest中请求头常用方法
方法 | 描述 |
---|---|
String getHeader(String var1) | 获取请求头的值 |
Enumeration |
获取所有请求头 |
请求头栗子
TestServlet4
@WebServlet("/Test4")
public class TestServlet4 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//请求头相关的栗子
Enumeration<String> es = req.getHeaderNames();
while (es.hasMoreElements()) {
System.out.println(es.nextElement());
}
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test4
//控制台输出
host
connection
sec-ch-ua
sec-ch-ua-mobile
sec-ch-ua-platform
upgrade-insecure-requests
user-agent
accept
sec-fetch-site
sec-fetch-mode
sec-fetch-user
sec-fetch-dest
accept-encoding
accept-language
cookie
HttpServletRequest中获取参数常用方法
方法 | 描述 |
---|---|
String getParameter(String var1) | 获取参数值 |
String[] getParameterValues(String var1) | 获取所有参数值 |
Enumeration |
获取所有参数与值的迭代器 |
Map<String, String[]> getParameterMap() | 获取所有参数与值的Map |
请求参数栗子
TestServlet4
@WebServlet("/Test4")
public class TestServlet4 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//请求参数相关的栗子
System.out.println(req.getParameter("uname"));
//某些参数不止一个值
System.out.println(Arrays.toString(req.getParameterValues("love")));
System.out.println("==================");
//迭代所有参数与值
Enumeration<String> es = req.getParameterNames();
while (es.hasMoreElements()) {
System.out.println(es.nextElement());
}
System.out.println("==============");
//获取参数与值的map
Map<String, String[]> map = req.getParameterMap();
Set<Map.Entry<String, String[]>> set = map.entrySet();
Iterator<Map.Entry<String, String[]>> iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String[]> entry = iterator.next();
String[] value = entry.getValue();
System.out.println(entry.getKey()+":"+Arrays.toString(value));
}
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test4?love=1&love=2&p=1
//控制台输出
null
[1, 2]
==================
love
p
==============
love:[1, 2]
p:[1]
因为并没有uname的参数传递,所以是null。
HttpServletResponse
Tomcat会自动将服务端响应报文封装为HttpServletResponse对象。
HttpServletResponse中常用方法
方法 | 描述 |
---|---|
void setStatus(int var1) | 设置状态码 |
void setHeader(String var1, String var2) | 设置响应头和值 |
void setContentType(String var1) | 设置响应头Content-Type的值 |
PrintWriter getWriter() throws IOException | 获取字符打印流 |
ServletOutputStream getOutputStream() throws IOException | 获取字节输出流 |
响应报文栗子
TestServlet4
@WebServlet("/Test4")
public class TestServlet4 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//响应报文常用请求方法,更多请查询相关文档!
resp.setStatus(200);
//默认Content-type值就是text/html
resp.setContentType("text/html");
resp.setHeader("a1","aaaaa");
//向响应体内输出字符,由于默认的Content-type值就是text/html,所以可以写HTML标签
PrintWriter writer = resp.getWriter();
writer.write("<h1>123</h1>");
//向响应体内输出字节
ServletOutputStream outputStream = resp.getOutputStream();
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test4
HttpServletRequest和HttpServletResponse的报文行区别
-
HttpServletRequest请求行
-
GET /JavaWeb_war_exploded/Test4 HTTP/1.1
-
请求方法 URL 请求协议及版本号
-
-
HttpServletResponse响应行
-
HTTP/1.1 200
-
请求协议及版本号 状态码
-