栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HttpServletRequest详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpServletRequest详解

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

postman测试:


打印结果:

authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjVmODEwMTJkLTlkYTUtNDcwMC1hNWUyLTk0NmQ2NjFlN2JhOCJ9.cycMQn6fMGQOymGhol7_dknpkFWD3uW7nwzlspqIBInTgJHTf4ROKjNLsETXo6el8IjrZk2HL6OtG78MKPQkgg
user-agent:PostmanRuntime/7.26.8
accept:*/*
postman-token:325bdf26-ec7f-47ad-b7f4-37d4f8b6fd8b
host:localhost:8080
accept-encoding:gzip, deflate, br
connection:keep-alive
content-length:0

会发现,打印结果就是postman中Header中的所有属性元素。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939502.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号