1

I am developing web application and i need to know who are all access my web application for that i need ip (ipv4) address of the system which runs my application . i am using jsp and servlet can any one help me to fix this ???

i tried the bellow code but it shows my ip address on every time when ever user access the application.but i need client ip (ipv4) address.

try {
                 Enumeration e = NetworkInterface.getNetworkInterfaces();

                 while(e.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) e.nextElement();
                    System.out.println("Net interface: "+ni.getName());

                    Enumeration e2 = ni.getInetAddresses();

                    while (e2.hasMoreElements()){
                       InetAddress ip = (InetAddress) e2.nextElement();
                       System.out.println("IP address: "+ ip.toString());
                    }
                 }
              }
              catch (Exception e) {
                 e.printStackTrace();
              }
KVK
  • 1,257
  • 2
  • 17
  • 48
  • `NetworkInterface.getNetworkInterfaces()` get the network interfaces where the JVM is running, your server. – PeterMmm Sep 18 '14 at 06:45
  • 1
    Your title says you want the client's IP, but your question says you want the "address of the system which runs my application", which would be the server. It's unclear which you want. Your code seems to be looking for the server's IP address, but that won't tell you who's accessing your application. – Wyzard Sep 18 '14 at 06:45
  • @Wyzard: He needs the client IP address (where "runs my application" implies "inside a browser"). – G B Sep 18 '14 at 06:52

4 Answers4

2

On the HttpServletRequest object, you can use the following function to get the remote host:

request.getRemoteHost()

Petter
  • 4,053
  • 27
  • 33
  • Most live systems have proxies, through which your client requests will reach the back end. In such an instance your method will return that proxy's details. – Thihara Sep 18 '14 at 06:47
  • True! In that case it should be possible to configure the proxy server to include the original host in a header. – Petter Sep 18 '14 at 06:50
  • @Petter it works fine in linux system but not work in windows why? – KVK Sep 18 '14 at 06:58
  • @Petter i use this it works fine in linux system because in our network linux systems are not in proxy but windows system it have a proxy so that it gives default ip address for all windows system what can i do – KVK Sep 18 '14 at 07:08
  • You can configure your proxy server to send the original IP in a header, and get that value to see the original host. – Petter Sep 18 '14 at 07:23
1
Normally, you can use servletRequest.getRemoteAddr() to get the client’s IP address that’s accessing your Java web application.

   String ipAddress = request.getRemoteAddr();

But, if user is behind a proxy server or access your web server through a load balancer (for example, in cloud hosting), the above code will get the IP address of the proxy server or load balancer server, not the original IP address of a client.

 String ipAddress = request.getHeader("X-FORWARDED-FOR");  
   if (ipAddress == null) {  
       ipAddress = request.getRemoteAddr();  
   }
0

There's several way to do this :

  • you can use HttpServletRequest#getRemoteAddr(), but it returns an IP address corresponding to a client or the last proxy, which may not be exactly what you need (if you really want the client, that is)
  • you can look at some HTTP headers like REMOTE_ADDR, HTTP_X_FORWARDED_FOR (list of IPs), HTTP_CLIENT_IP, HTTP_X_FORWARDED, HTTP_X_CLUSTER_CLIENT_IP, HTTP_FORWARDED_FOR and HTTP_FORWARDED. the xxx_forwarded_ headers should give you the real IP address (and not the proxy) but might be empty (some proxy do not forward this header, for example).

Beware though that these ip can be easily spoofed and should not be relied on for anything critical (security, etc.)

Pierre Rust
  • 2,474
  • 18
  • 15
0

You can check this way as well,

public String getRemoteIpAddress(HttpServletRequest req) {      
 String ip = req.getHeader("x-forwarded-for");      
  if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {      
     ip = req.getHeader("Proxy-Client-IP");      
  }      
    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {      
     ip = req.getHeader("WL-Proxy-Client-IP");      
  }      
  if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {      
      ip = req.getRemoteAddr();      
  }      
  return ip;      
}   

if user is behind a proxy server or access your web server through a load balancer (for example, in cloud hosting), the above code will get the IP address of the proxy server or load balancer server, not the original IP address of a client.

To solve it, you should get the IP address of the request’s HTTP header “X-Forwarded-For (XFF)“.

 //is client behind something?
 String ipAddress = request.getHeader("X-FORWARDED-FOR");  
 if (ipAddress == null) {  
     ipAddress = request.getRemoteAddr();  
 }
Subh
  • 414
  • 6
  • 14
  • i use the above code it works fine in linux system because in our network linux systems are not in proxy but windows system it have a proxy so that it gives default ip address for all windows system what can i do – KVK Sep 18 '14 at 07:07
  • I have updated the answer, please check although I dont have Windows machine with me. So could not check. Confirm !! – Subh Sep 18 '14 at 07:12