`

获取客户端浏览器和操作系统信息

 
阅读更多
利用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(枚举)类的实例

获取IP
package com.slei.ip;

import javax.servlet.http.HttpServletRequest;

public class TestIP {
    public TestIP() {
    }

    public String getIpAddrOne(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

public String getIpAddrTwo(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
     ip = request.getHeader("Proxy-Client-IP");
         }
       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
         }
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
               ip = request.getRemoteAddr();
           }
          return ip;
      }

}


<%@ page contentType="text/html; charset=GBK" %>
<%@page import="java.util.*"%>
<html>
<head>
<title>
get_ip
</title>
</head>
<body bgcolor="#ffffff">
<jsp:useBean id="iptest" scope="page" class="com.slei.ip.TestIP"></jsp:useBean>
<h1 align="center">
YOU ADDRESS IS:<%out.print(iptest.getIpAddrOne(request));%><br/>
YOU ADDRESS IS:<%out.print(iptest.getIpAddrTwo(request));%>
</h1>

<table border=0 align=center cellpadding=0 cellspacing=1 bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">Name</td>
<td bgcolor="#FFFFFF">Value</td>
</tr>
<%
Enumeration enumNames;
String strName,strValue;
enumNames = request.getHeaderNames();
while(enumNames.hasMoreElements()){
    strName = (String)enumNames.nextElement();
    strValue = request.getHeader(strName);
    %>
    <tr>
   <td bgcolor="#FFFFFF"><%=strName%></td>
   <td bgcolor="#FFFFFF"><%=strValue%></td>
   </tr>
   <%
}
%>

</table>
</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics