//获取请求方式
System.out.println(req.getMethod());
//获取项目名称
System.out.println(req.getContextPath());
//获取完整请求路径
System.out.println(req.getRequestURL());
//获取除了域名外的请求数据
System.out.println(req.getRequestURI());
//获取请求参数
System.out.println(req.getQueryString());
System.out.println("----------------------------------------------------------");
//获取请求头
String header = req.getHeader("user-Agent");
System.out.println(header);
header = header.toLowerCase();
//根据请求头数据判断浏览器类型
if(header.contains("chrome")){
System.out.println("您的访问浏览器为谷歌浏览器");
}else if(header.contains("msie")){
System.out.println("您的访问浏览器为IE浏览器");
}else if(header.contains("firefox")){
System.out.println("您的访问浏览器为火狐浏览器");
}else{
System.out.println("您的访问浏览器为其它浏览器");
}
System.out.println("----------------------------------------------------------");
//获取所有的消息头名称
Enumeration headerNames = req.getHeaderNames();
//获取获取的消息头名称,获取对应的值,并输出
while(headerNames.hasMoreElements()){
String nextElement = headerNames.nextElement();
System.out.println(nextElement+":"+req.getHeader(nextElement));
}
System.out.println("----------------------------------------------------------");
//根据名称获取此重名的所有数据
Enumeration headers = req.getHeaders("accept");
while (headers.hasMoreElements()) {
String string = (String) headers.nextElement();
System.out.println(string);
}
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
结果如下:
GET
/httpServletRequest
http://localhost:8080/httpServletRequest/index1
/httpServletRequest/index1
name=ckinghan&password=admin
----------------------------------------------------------
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
您的访问浏览器为谷歌浏览器
----------------------------------------------------------
host:localhost:8080
connection:keep-alive
cache-control:max-age=0
upgrade-insecure-requests:1
user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding:gzip, deflate, sdch, br
accept-language:zh-CN,zh;q=0.8
----------------------------------------------------------
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
---------------------
作者:ckinghan58
来源:CSDN
原文:https://blog.csdn.net/ckinghan58/article/details/73557414/
版权声明:本文为博主原创文章,转载请附上博文链接!