JSP里的request.getHeader("user-agent")小结

利用request.getHeader("user-agent")获取客户端浏览器和操作系统信息 String Agent = request.getHeader("User-Agent");
StringTokenizer st = new StringTokenizer(Agent,";");
st.nextToken();
//得到用户的浏览器名
String userbrowser = st.nextToken();
//得到用户的操作系统名
String useros = st.nextToken();取得本机的信息也可以这样:操作系统信息
System.getProperty("os.name");           //win2003竟然是win XP?
System.getProperty("os.version");
System.getProperty("os.arch");
浏览器:
request.getHeader("User-Agent")
request.getHeader(“User-agent”)返回客户端浏览器的版本号、类型getHeader(String name):获得http协议定义的传送文件头信息,request. getMethod():获得客户端向服务器端传送数据的方法有GET、POST、PUT等类型request. getRequestURI():获得发出请求字符串的客户端地址request. getServletPath():获得客户端所请求的脚本文件的文件路径request. getServerName():获得服务器的名字request.getServerPort():获得服务器的端口号request.getRemoteAddr():获得客户端的IP地址request.getRemoteHost():获得客户端电脑的名字,若失败,则返回客户端电脑的IP地址request.getProtocol():request.getHeaderNames():返回所有request header的名字,结果集是一个Enumeration(枚举)类的实例request.getHeaders(String name):返回指定名字的request header的所有值,结果集是一个Enumeration(枚举)类的实例

Of all the headers sent by the browser, the User-Agent header is probably the most useful because it indicates what kind of browser is making the request. Oddly, both Netscape and Internet Explorer identify themselves as Mozilla, which was the nickname for the early Netscape browser. (There is now a browser called Mozilla, which was born from the code for Netscape 5.)   In case you're wondering where the name Mozilla came from, Netscape was founded by the folks who wrote the old Mosaic Web browser. Netscape Navigator was intended to be a monstrous version of Mosaic: the Godzilla Mosaic, or Mozilla.   When Internet Explorer (IE) first came out, it lagged behind Netscape in usage, and gradually added features to become a reasonable alternative by the time IE version 3 came along. By identifying itself as Mozilla-compatible, IE is telling the Web server that it can handle anything Mozilla can.   If you want to figure out whether the browser is Netscape or IE, only IE sends the MSIE string as part of its User-Agent header. Thus, you can do the following test in your JSP or servlet:

if (request.getHeader("USER-AGENT"). indexOf("MSIE") >= 0) { // do Internet Explorer specific stuff here } else { // do Netscape specific stuff here }
  You can perform similar tests to detect other browsers, such as Opera.
  研究到这里,其实到底USER-AGENT包含哪些元素,自己还不是太清楚,于是写了下面的代码来查看所有的文件头里都有什么:
Enumeration temp=request.getHeaderNames();   while (temp.hasMoreElements()){    String paramName = (String) temp.nextElement();   out.print(paramName+"=");   out.println(request.getHeader(paramName)+"<br>"); }
  在IE浏览器中得到的是:
Accept=*/*
Accept-Language=zh-cn
Accept-Encoding=gzip, deflate
User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
Host=localhost:8080
Connection=Keep-Alive
Cookie=JSESSIONID=1zxeFTZUY1grAeAi

  在Firefox中得到的是:
Host=localhost:8080
User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
Accept=text/xml,application/xml,application/xhtml+xml,text/html;
q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language=zh-cn,zh;q=0.5
Accept-Encoding=gzip,deflate
Accept-Charset=gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive=300
Connection=keep-alive
Cookie=JSESSIONID=3wYp8TfGUnbgfxAi
Cache-Control=max-age=0

  在Opera中得到的是:
User-Agent=Opera/8.01 (Windows NT 5.1; U; zh-cn)
Host=localhost:8080
Accept=text/html, application/xml;q=0.9, 
application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language=zh-cn,en;q=0.9
Accept-Charset=gbk, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1
Accept-Encoding=deflate, gzip, x-gzip, identity, *;q=0
Cookie=JSESSIONID=3JsNHSp6bxg_0KAi
Cookie2=$Version=1
Cache-Control=no-cache
Connection=Keep-Alive, TE
TE=deflate, gzip, chunked, identity, trailers

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值