【Jsp/Servlet】获取客户端使用的ip
一般使用jsp的时候大多数时间都可以使用request.getRemoteAddr() 来获取ip,但是这个前提是未经过反向代理等操作的原始地址,所以,需要在反向代理等操作之后还要获取客户端的ip变得困难重重,这里给大家个方法,仅供参考
1 //获取客户端真实注册地址 2 public String getRemoteHost(HttpServletRequest request){ 3 String ip = request.getHeader("x-forwarded-for"); 4 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 5 ip = request.getHeader("Proxy-Client-IP"); 6 } 7 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 8 ip = request.getHeader("WL-Proxy-Client-IP"); 9 } 10 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ 11 ip = request.getRemoteAddr(); 12 } 13 return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip; 14 }