HttpServletRequest 详解
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
HttpServletRequest 接口继承自 ServletRequest 接口
,其主要作用是封装 HTTP 请求消息
。由于 HTTP 请求消息分为请求行
、请求消息头
和请求消息体
三部分。因此,在 HttpServletRequest 接口中定义了获取请求行、请求头和请求消息体
的相关方法。
客户端:客户端(Client)或称为用户端,是指与服务器相对应,为客户提供本地服务的程序。如万维网使用的网页浏览器
,收寄电子邮件时的电子邮件客户端
,以及即时通讯的客户端软件
等。
请求头信息信息,是指postman中Header中的内容:
获取请求行信息的相关方法
当访问 Servlet 时,所有请求消息将被封装到 HttpServletRequest 对象中,请求消息的请求行中包含请求方法
、请求资源名
、请求路径
等信息,为了获取这些信息,HttpServletRequest 接口定义了一系列方法:
HttpServletRequest获取请求行的相关信息:
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/path")
public class PersonController {
@PostMapping("/person/add")
public void addPerson(HttpServletRequest request) {
System.out.println("getMethod : " + request.getMethod());
System.out.println("getRequestURI:" + request.getRequestURI());
System.out.println("getQueryString:" + request.getQueryString());
System.out.println("getContextPath:" + request.getContextPath());
System.out.println("getServletPath:" + request.getServletPath());
System.out.println("getRemoteAddr : " + request.getRemoteAddr());
System.out.println("getRemoteHost : " + request.getRemoteHost());
System.out.println("getRemotePort : " + request.getRemotePort());
System.out.println("getLocalAddr : " + request.getLocalAddr());
System.out.println("getLocalName : " + request.getLocalName());
System.out.println("getLocalPort : " + request.getLocalPort());
System.out.println("getServerName : " + request.getServerName());
System.out.println("getServerPort : " + request.getServerPort());
System.out.println("getRequestURL : " + request.getRequestURL());
}
}
postman测试:
打印结果:
getMethod : POST
getRequestURI:/path/person/add
getQueryString:null
getContextPath:
getServletPath:/path/person/add
getRemoteAddr : 0:0:0:0:0:0:0:1
getRemoteHost : 0:0:0:0:0:0:0:1
getRemotePort : 63277
getLocalAddr : 0:0:0:0:0:0:0:1
getLocalName : 0:0:0:0:0:0:0:1
getLocalPort : 8080
getServerName : localhost
getServerPort : 8080
getRequestURL : http://localhost:8080/path/person/add
获取请求消息头的相关方法
当浏览器发送 Servlet 请求时,需要通过请求消息头向服务器传递附加信息
,例如,客户端可以接收的数据类型、压缩方式、语言等。为此,在 HttpServletRequest 接口中定义了一系列用于获取 HTTP 请求头字段的方法:
读取 HTTP 请求消息头字段:
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
@RestController
@RequestMapping("/path")
public class PersonController {
@PostMapping("/person/add")
public void addPerson(HttpServletRequest request) {
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = (String) headerNames.nextElement();
System.out.println(headerName + ":" + request.getHeader(headerName)
+ "<br />");
}
}
}
postman测试:
打印结果:
authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjVmODEwMTJkLTlkYTUtNDcwMC1hNWUyLTk0NmQ2NjFlN2JhOCJ9.cycMQn6fMGQOymGhol7_dknpkFWD3uW7nwzlspqIBInTgJHTf4ROKjNLsETXo6el8IjrZk2HL6OtG78MKPQkgg<br />
user-agent:PostmanRuntime/7.26.8<br />
accept:*/*<br />
postman-token:325bdf26-ec7f-47ad-b7f4-37d4f8b6fd8b<br />
host:localhost:8080<br />
accept-encoding:gzip, deflate, br<br />
connection:keep-alive<br />
content-length:0<br />
会发现,打印结果就是postman中Header中的所有属性元素。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)