Request

Reuqest原理

1
2
3
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service...");
}
  1. tomcat服务器会根据请求url中的资源路径,创建对应的ServletDemo1的对象

  2. tomcat服务器,会创建request和response对象,request对象中封装请求消息数据

  3. tomcat将request和response两个对象传递给service方法,并且调用service方法

  4. 程序员可以通过request对象来获取请求消息数据,可以通过response对象来设置响应消息数据

  5. 服务器在给浏览器做出响应之前,会从response对象中拿程序员设置的响应消息数据

request与response对象

  1. request和response对象是由服务器创建的。程序员使用它们

  2. request对象是来获取请求消息,response对象是来设置响应消息

继承体系结构

1
2
3
4
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request);
}
}

当访问时就会输出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
4
String getRequestURL()

返回结果为
http://localhost/day14/demo3

获取协议及版本

使用String getProtocol()方法来获取

获取客户机IP地址

使用String getRemoteAddr()方法来获取

实例

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package 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;

/**
* @author 51nd0re1
*/
@WebServlet(name = "RequestDemo1",urlPatterns = "/request1")
public class RequestDemo1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("POST");
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求方法
String method=request.getMethod();
System.out.println(method);
//2.获取虚拟目录
String context = request.getContextPath();
System.out.println(context);
//3.获取Servletl路径
String servletPath = request.getServletPath();
System.out.println(servletPath);
//4.获取get方式请求参数
String queryString = request.getQueryString();
System.out.println(queryString);
//5.获取请求URI/URL
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
//6.获取版本
String protocal = request.getProtocol();
System.out.println(protocal);
//7.获取客户机IP
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}

访问http://localhost:8080/hello/request1?name=51nd0re1

运行结果

1
2
3
4
5
6
7
8
GET
/hello
/request1
name=51nd0re1
/hello/request1
http://localhost:8080/hello/request1
HTTP/1.1
0:0:0:0:0:0:0:1

获取请求头数据

getHeader(String name)```
1
2
3
4
通过请求头的名称获取请求头的值


```Enumeration<String> getHeaderNames()

获取所有的请求头名称

实例

访问之后

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
30
package 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;

@WebServlet(name = "RequestDemo2",urlPatterns = "/request2")
public class RequestDemo2 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

@Override
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
8
host---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-agent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package 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;

@WebServlet(name = "RequestDemo3",urlPatterns = "/request3")
public class RequestDemo3 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

@Override
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
27
package 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;

@WebServlet(name = "RequestDemo3",urlPatterns = "/request3")
public class RequestDemo3 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

@Override
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
28
package 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;

@WebServlet(name = "RequestDemo4",urlPatterns = "/request4")
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

需要通过连接来访问,则运行结果

avatar

点击访问之后,运行结果

1
2
http://localhost:8080/hello/get.html
安全访问

获取请求体数据

请求体只有POST请求方式才会有请求体,在请求体中封装了POST请求的请求参数

步骤:

  1. 获取流对象
  2. 再从流对象中拿对象

获取流对象

1
BufferedReader getReader():获取字符输入流,只能操作字符数据
1
ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据

创建regist.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<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
28
package 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;

@WebServlet(name = "RequestDemo5",urlPatterns = "/request5")
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