How to get client Ip Address in Java HttpServletRequest
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();
}
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.
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/