天天看點

J2EE中擷取IP位址

在jsp裡,擷取用戶端的ip位址的方法是request.getremoteaddr(),這種方法在大部分情況下都是有效的。但是在通過了 apache,squid等反向代理軟體就不能擷取到用戶端的真實ip位址了。如果使用了反向代理軟體,用 request.getremoteaddr()方法擷取的ip位址是:127.0.0.1或 192.168.1.110,而并不是用戶端的真實ip。

protected string getipaddr() {  

    httpservletrequest request = servletactioncontext.getrequest();  

    string ip = request.getheader("x-forwarded-for");  

    if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip)) {  

        ip = request.getheader("proxy-client-ip");  

    }  

        ip = request.getheader("wl-proxy-client-ip");  

        ip = request.getremoteaddr();  

    return ip;  

原帖位址:http://blog.csdn.net/smcwwh/article/details/6255324