javaweb学习(一):获得HttpServletRequest所有请求消息

HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
 其实只要知道,HTTP的所有请求信息都会在这个对象中,就很容易获取各种各样的信息了,这可以为以后搭建架构的时候,建立很重要的基础。

直接写代码,写一个controller,把常用信息都打印出来就知道了:

package controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/request")
public class RequestController {
	

	
	
	   @RequestMapping(value="/getInfo",method = RequestMethod.GET)
	    public String upload(HttpServletRequest request){

		   System.out.println("Protocol: " + request.getProtocol());
		    System.out.println("Scheme: " + request.getScheme());
		    System.out.println("Server Name: " + request.getServerName());
		    System.out.println("Server Port: " + request.getServerPort());
		    System.out.println("Protocol: " + request.getProtocol());
		    System.out.println("Remote Addr: " + request.getRemoteAddr());
		    System.out.println("Remote Host: " + request.getRemoteHost());
		    System.out.println("Character Encoding: " + request.getCharacterEncoding());
		    System.out.println("Content Length: " + request.getContentLength());
		    System.out.println("Content Type: " + request.getContentType());
		    System.out.println("Auth Type: " + request.getAuthType());
		    System.out.println("HTTP Method: " + request.getMethod());
		    System.out.println("Path Info: " + request.getPathInfo());
		    System.out.println("Path Trans: " + request.getPathTranslated());
		    System.out.println("Query String: " + request.getQueryString());
		    System.out.println("Remote User: " + request.getRemoteUser());
		    System.out.println("Session Id: " + request.getRequestedSessionId());
		    System.out.println("Request URI: " + request.getRequestURI());
		    System.out.println("Servlet Path: " + request.getServletPath());
		    System.out.println("Accept: " + request.getHeader("Accept"));
		    System.out.println("Host: " + request.getHeader("Host"));
		    System.out.println("Referer : " + request.getHeader("Referer"));
		    System.out.println("Accept-Language : " + request.getHeader("Accept-Language"));
		    System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding"));
		    System.out.println("User-Agent : " + request.getHeader("User-Agent"));
		    System.out.println("Connection : " + request.getHeader("Connection"));
		    System.out.println("Cookie : " + request.getHeader("Cookie"));
		    System.out.println("Created : " + request.getSession().getCreationTime());
		    System.out.println("LastAccessed : " + request.getSession().getLastAccessedTime());

	        return null;


	    }
	    
	 
}



打印出来的消息:

Protocol: HTTP/1.1
Scheme: http
Server Name: localhost
Server Port: 8080
Protocol: HTTP/1.1
Remote Addr: 0:0:0:0:0:0:0:1
Remote Host: 0:0:0:0:0:0:0:1
Character Encoding: UTF-8
Content Length: -1
Content Type: null
Auth Type: null
HTTP Method: GET
Path Info: null
Path Trans: null
Query String: null
Remote User: null
Session Id: null
Request URI: /u3-ssm-t51//request/getInfo
Servlet Path: /request/getInfo
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */*
Host: localhost:8080
Referer : null
Accept-Language : zh-CN
Accept-Encoding : gzip, deflate
User-Agent : Mozilla/5.0 (Windows NT 6.2; Win64; x64; Trident/7.0; rv:11.0) like Gecko
Connection : Keep-Alive
Cookie : null
Created : 1542116352763
LastAccessed : 1542116352763

在未来的架构设计中,可以利用反射,根据request的信息进行封装成自己想要的信息,然后进行相关验证。

HttpServletRequest的原理,后续会进行相关讲解

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值