1.读取http请求头信息
使用HttpRequest中的方法
*一般方法:
getHeader
getHeaders
getHeaderNames
*专门方法:
getCookies
getAuthType
......
获得所有的请求头信息的代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Enumeration<String> enum1 = request.getHeaderNames();
while(enum1.hasMoreElements()){
String name = enum1.nextElement();
String value = request.getHeader(name);
System.out.println(name+":"+value);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
输出结果:
15:01:26,718 INFO [STDOUT] accept:application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
15:01:26,718 INFO [STDOUT] accept-language:zh-CN
15:01:26,718 INFO [STDOUT] accept-encoding:gzip, deflate
15:01:26,719 INFO [STDOUT] user-agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)
15:01:26,719 INFO [STDOUT] host:localhost:8080
15:01:26,719 INFO [STDOUT] connection:Keep-Alive
15:01:26,719 INFO [STDOUT] cookie:JSESSIONID=657E2CE8E6964CC13943A7D227C88779
注:用不同的浏览器输出的内容不同。
2.根据user-agent来判断所使用的浏览器,代码内容如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//判断所使用的浏览器
String userAgent = request.getHeader("user-agent");
if(userAgent.indexOf("MSIE")!=-1){
System.out.println("您当前使用的是IE浏览器");
}else{
System.out.println("您当前使用的是其他浏览器");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}