There are many applications that require access to the IP address of the visitor, such as preventing unauthorized users or malicious users from accessing them. Java EE's servlet standard interface Javax.servlet.http.HttpServletRequest provides the GETREMOTEADDR () method for obtaining the visitor's IP address.
REQUEST.GETREMOTEADDR () This approach is valid for most cases of IP access. However, the client can not get the real IP address of the clients through to the agent software. After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, in the forwarding request HTTP header information, the X-forwarded-for information is added to track the original client IP address and the server address of the original client request. So want to get the real IP of the client must first Judge Request.getheader ();
Java gets the IP address code:
PUBLIC STATIC STRING GETIPADDR (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; }
Reference:
http://xiaoboss.iteye.com/blog/1181488
Http://www.cnblogs.com/xuekyo/archive/2012/05/17/2505772.html
Java Get IP Address