40

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.

Max
  • 915
  • 10
  • 28
Samith Dilshan
  • 453
  • 1
  • 5
  • 8
  • There is no accurate way to get the client ip address unless the client explicitly sends it to you – Scary Wombat Apr 28 '15 at 04:10
  • 1
    There is not way to get the client's real Ip address what you actually see there is your public IP address. – Rod_Algonquin Apr 28 '15 at 04:11
  • 2
    possible duplicate of [Is it possible to accurately determine the IP address of a client in java servlet](http://stackoverflow.com/questions/9326138/is-it-possible-to-accurately-determine-the-ip-address-of-a-client-in-java-servle) – YoYo Apr 28 '15 at 04:17
  • It means there are no way for a web app to accurately determine the client IP address????? – Samith Dilshan Apr 28 '15 at 04:30
  • 3
    The last option is wrong. It returns the ip of your server. You think its correct because in a development environment, your server and the browser you test it from are co-located. – ramp Apr 28 '15 at 10:59

4 Answers4

89

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/

Max
  • 915
  • 10
  • 28
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • 6
    When I use the above code, I'm getting 'localhost' not my ip address. – Rama Krishna Jul 28 '17 at 06:33
  • 38
    X-FORWARD-FOR header is a bit risky here. Because if you use reverse proxy for request, this header value will be like this: 231.23.45.65, 10.20.10.33, 10.20.20.34 (which means client ip, load balancer server, reverse proxy server). Check if your X-FORWARD-FOR header value contains ',' or not. `ipAddress.contains(",") ? ipAddress.split(",")[0] : ipAddress` – utkusonmez Aug 23 '17 at 07:14
  • Agreed. Reasonable reference (as of 2023 March) is here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For – Scott Corscadden Mar 08 '23 at 17:23
32

Try this one. for all condition

private static final String[] HEADERS_TO_TRY = {
            "X-Forwarded-For",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_X_FORWARDED",
            "HTTP_X_CLUSTER_CLIENT_IP",
            "HTTP_CLIENT_IP",
            "HTTP_FORWARDED_FOR",
            "HTTP_FORWARDED",
            "HTTP_VIA",
            "REMOTE_ADDR" };

private String getClientIpAddress(HttpServletRequest request) {
    for (String header : HEADERS_TO_TRY) {
        String ip = request.getHeader(header);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
    }

    return request.getRemoteAddr();
}
Max
  • 915
  • 10
  • 28
elaheh aghaee
  • 321
  • 3
  • 3
  • 20
    It's always a good idea to add some explanation to an answer so the OP can actually learn something. – trotta Jul 07 '19 at 08:46
2

In case, you are trying to get the IP-address for Dev-environment then you can use this:-

public String processRegistrationForm(HttpServletRequest request)
{
    String appUrl = request.getScheme() + "://"+ request.getLocalAddr();
    return appUrl;
}

The request.getLocalAddr() will return the IP-address of the request receiving system.

Hope it helps.

sajas
  • 1,599
  • 1
  • 17
  • 39
-5
 import java.net.UnknownHostException;

/**
 * Simple Java program to find IP Address of localhost. This program uses
 * InetAddress from java.net package to find IP address.
 *
 */
public class IPTest { 

public static void main(String args[]) throws UnknownHostException {

    InetAddress addr = InetAddress.getLocalHost();

    //Getting IPAddress of localhost - getHostAddress return IP Address
    // in textual format
    String ipAddress = addr.getHostAddress();

    System.out.println("IP address of localhost from Java Program: " + ipAddress);

    //Hostname
    String hostname = addr.getHostName();
    System.out.println("Name of hostname : " + hostname);     
}
}

Output:

IP address of localhost from Java Program: 190.12.209.123
Name of hostname : PCLOND3433
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
bhargav
  • 11