编程语言 php java

JSP获取IP地址

java HTML我帮您 1年前  0次浏览
JSP获取客户端IP是一个基础知识,经常要用到,用JSP得到IP分两种情况,一种是获取代理服务器上网的IP,另一种是直接上网的,因此来说,根据你的需要,代码也需要分开进行:
public static String getRequestIP(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.getRemoteAddr();
}
return ip;
}

版权所有

发表评论