Request
Reuqest原理
1 | public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { |
tomcat服务器会根据请求url中的资源路径,创建对应的ServletDemo1的对象
tomcat服务器,会创建request和response对象,request对象中封装请求消息数据
tomcat将request和response两个对象传递给service方法,并且调用service方法
程序员可以通过request对象来获取请求消息数据,可以通过response对象来设置响应消息数据
服务器在给浏览器做出响应之前,会从response对象中拿程序员设置的响应消息数据
request与response对象
request和response对象是由服务器创建的。程序员使用它们
request对象是来获取请求消息,response对象是来设置响应消息
继承体系结构
1 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
当访问时就会输出request对象
运行结果1
org.apache.catalina.connector.RequestFacade@799ee87
可以看到是apache包下的对象,@后是hash值
体系结构为:
ServletRequest —接口
HttpServletRequest —接口
org.apache.catalina.connector.RequestFacade 类(tomcat)
请求数据
获取请求消息数据可以有
获取请求行数据
获取请求头数据
获取请求体数据
获取请求行数据
例如
GET /day14/demo3?name=51nd0re1 HTTP/1.1
请求方式为 GET
使用String getMethod()
方法来获取
*获取虚拟目录(/day14)
使用String getContextPath()
方法来获取
获取Servlet路径(/demo3)
使用String getServletPath()
方法来获取
获取get方式的请求参数(name=51nd0re1)
使用String getQueryString()
方法来获取
*获取请求URI(/day14/demo3)
使用String getRequestURI()
方法来获取
还有一个获取URL的方法1
2
3
4String getRequestURL()
返回结果为
http://localhost/day14/demo3
获取协议及版本
使用String getProtocol()
方法来获取
获取客户机IP地址
使用String getRemoteAddr()
方法来获取
实例
1 | package Request; |
访问http://localhost:8080/hello/request1?name=51nd0re1
运行结果1
2
3
4
5
6
7
8GET
/hello
/request1
name=51nd0re1
/hello/request1
http://localhost:8080/hello/request1
HTTP/1.1
0:0:0:0:0:0:0:1
获取请求头数据
1 | 通过请求头的名称获取请求头的值 |
获取所有的请求头名称
实例
访问之后1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
"RequestDemo2",urlPatterns = "/request2") (name =
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//2.遍历
while(headerNames.hasMoreElements()){
String name = headerNames.nextElement();
//根据名称获取请求头的值
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}
}
}
运行结果1
2
3
4
5
6
7
8host---localhost:8080
user-agent---Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0
accept---text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-language---zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
accept-encoding---gzip, deflate
connection---keep-alive
cookie---JSESSIONID=C4074C701516CDB3FA95402652CB489D; Phpstorm-d069594e=0d583fa6-ad80-4735-a385-c10cee7ee32f
upgrade-insecure-requests---1
如果想要获取单个头信息,使用getHeader()就行
例如获取user-agent1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
"RequestDemo3",urlPatterns = "/request3") (name =
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取user-agent
String UA = request.getHeader("user-agent");
System.out.println(UA);
}
}
运行结果1
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0
还可以借此来判断浏览器1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
"RequestDemo3",urlPatterns = "/request3") (name =
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取user-agent
String UA = request.getHeader("user-agent");
if(UA.contains("Chrome")){
System.out.println("Chrome浏览器");
}else if(UA.contains("Firefox")){
System.out.println("Firefox浏览器");
}
}
}
这样就可以判断出是什么浏览器访问的
可以使用referer来防盗链1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
"RequestDemo4",urlPatterns = "/request4") (name =
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String referer = request.getHeader("referer");
System.out.println(referer);
if(referer != null){
if(referer.contains("/hello")){
System.out.println("安全访问");
}else{
System.out.println("不安全访问");
}
}
}
}
如果直接访问就会输出null
需要通过连接来访问,则运行结果
点击访问之后,运行结果1
2http://localhost:8080/hello/get.html
安全访问
获取请求体数据
请求体只有POST请求方式才会有请求体,在请求体中封装了POST请求的请求参数
步骤:
- 获取流对象
- 再从流对象中拿对象
获取流对象
1 | BufferedReader getReader():获取字符输入流,只能操作字符数据 |
1 | ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据 |
创建regist.html1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/hello/request5" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="password" placeholder="请输入密码" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
通过这个regist.html访问1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
"RequestDemo5",urlPatterns = "/request5") (name =
public class RequestDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求消息体--请求参数
//1.获取字符流
BufferedReader br=request.getReader();
//2.读取数据
String line = null;
while((line = br.readLine())!= null ){
System.out.println(line);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
运行结果1
username=51nd0re1&password=123