Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the remote address of a client in servlet?

Is there any way that I could get the original IP address of the client coming to the server? I can use request.getRemoteAddr(), but I always seem to get the IP of the proxy or the web server.

I would want to know the IP address that the client is using to connect to me. Is there anyway that I could get it?

like image 377
grassbl8d Avatar asked Jan 13 '11 10:01

grassbl8d


People also ask

How do we get the IP address of a client in a servlet?

In Java, you can use HttpServletRequest. getRemoteAddr() to get the client's IP address that's accessing your Java web application.

Which method returns the client IP address from ServletRequest?

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

How do I find my Httprequest IP address?

You can use RemoteAddr to get the remote client's IP address and port (the format is "IP:port"), which is the address of the original requestor or the last proxy (for example a load balancer which lives in front of your server). This is all you have for sure. This is because internally http. Header.


1 Answers

try this:

public static String getClientIpAddr(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.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();           }           return ip;       }   
like image 96
Fareed Alnamrouti Avatar answered Sep 19 '22 04:09

Fareed Alnamrouti