7

I need to get the IP address of the client in the JSP page. I have tried the following ways:

request.getRemoteAddr()
request.getHeader("X_FORWARDED_FOR")
request.getHeader("HTTP_CLIENT_IP")
request.getHeader("WL-Proxy-Client-IP")
request.getHeader("Proxy-Client-IP")
request.getHeader("REMOTE_ADDR")

However, none of those ways did return the desired IP address. How do I get the IP address of the client in the JSP page?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Satish Sharma
  • 3,284
  • 9
  • 38
  • 51

4 Answers4

13

To get IP address of the client, I've used the following method

<%   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.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }
        %>

Hope this helps, please leave a feed back.

8
<%
out.print( request.getRemoteAddr() );
out.print( request.getRemoteHost() );
%>

You may not get the real client IP if a the client is behind a proxy, you will get the IP of the proxy and not the client. However, the proxy may include the requesting client IP in a special HTTP header.

<%
out.print( request.getHeader("x-forwarded-for") );
%>
kinaesthesia
  • 703
  • 6
  • 27
3

Is your application server behind a load balancer, a proxy or a web server? Just an example; F5 load balancer exposes the client IP address with the "rlnclientipaddr" header:

request.getHeader("rlnclientipaddr");
orique
  • 1,295
  • 1
  • 27
  • 36
0

do you use reverse proxy like apache proxy? http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

When acting in a reverse-proxy mode (using the ProxyPass directive, for example), mod_proxy_http adds several request headers in order to pass information to the origin server. These headers are:

X-Forwarded-For
The IP address of the client.
X-Forwarded-Host
The original host requested by the client in the Host HTTP request header.
X-Forwarded-Server
The hostname of the proxy server.
Andrey Borisov
  • 3,160
  • 18
  • 18
  • No Proxy is used between server and client . All Values getting using X-Forwarded-For , X-Forwarded-Host , X-Forwarded-Server are giving null cause of proxy is not used. – Satish Sharma Jul 27 '12 at 07:38
  • if you are not using any of reverse proxy, just check use http servlet API - httpServletRequest.getRemoteHost() and getRemotePOrt() http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html – Andrey Borisov Jul 27 '12 at 07:39
  • Nothing is present in httpServletRequest that will provide me the IP of Client. I Have Checked The Whole API. – Satish Sharma Jul 27 '12 at 07:47