Importjavax.servlet.http.HttpServletRequest; Public classremoteaddress { Public Staticstring Getrealclientip (HttpServletRequest request) {string IP= Request.getheader ("X-forwarded-for"); SYSTEM.OUT.PRINTLN (IP); if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Proxy-client-ip"); SYSTEM.OUT.PRINTLN (IP); } if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP= Request.getheader ("Wl-proxy-client-ip"); SYSTEM.OUT.PRINTLN (IP); } if(IP = =NULL|| Ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {IP=request.getremoteaddr (); SYSTEM.OUT.PRINTLN (IP); } returnIP; }}
Before using the code as above to find that the results are 127.0.0.1, using this machine to do the server hanging online relay is also. Think it is code problems, and then directly hanging to the space, found that the normal display out of the network, and then use the mobile network to access the local server can also display IP is normal and no longer 127.0.0.1 or:: 1 of the IPV6 address.
JSP gets network IP address