HttpServletRequest详解

HttpServletRequest 详解

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。

HttpServletRequest 接口继承自 ServletRequest 接口,其主要作用是封装 HTTP 请求消息。由于 HTTP 请求消息分为请求行请求消息头请求消息体三部分。因此,在 HttpServletRequest 接口中定义了获取请求行、请求头和请求消息体的相关方法。

客户端:客户端(Client)或称为用户端,是指与服务器相对应,为客户提供本地服务的程序。如万维网使用的网页浏览器,收寄电子邮件时的电子邮件客户端,以及即时通讯的客户端软件等。

请求头信息信息,是指postman中Header中的内容:
postman请求头

获取请求行信息的相关方法

当访问 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测试:

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();
        //用循环遍历所有请求头,并通过 getHeader() 方法获取一个指定名称的头字段
        while (headerNames.hasMoreElements()) {
            String headerName = (String) headerNames.nextElement();
            System.out.println(headerName + ":" + request.getHeader(headerName)
                    + "<br />");
        }
    }
}

postman测试:

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中的所有属性元素。

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YD_1989

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值