I am trying to develop a Java web application (Servlet) which I need to get clients IP address.
Following is my code so far:
String ipAddress = request.getRemoteAddr();
In this case most of the time I get the 'Default gateway address' (147.120.1.5). Not my machine IP address(174.120.100.17).
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
In this case most of the time I get the 'Default gateway address' (147.120.1.5). Not my machine IP address (174.120.100.17).
InetAddress IP=InetAddress.getLocalHost();
System.out.println(IP.getHostAddress());
In this case I got the server IP Address (147.120.20.1).
My IP address in 147.120.100.17. Now I don't know how to get the real client IP address.
Thank you very much.
Try this one,
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
reference : http://www.mkyong.com/java/how-to-get-client-ip-address-in-java/