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()
}
out.print(ip)
%>
jsp获得本机的公网ip可以使用jsp中的request对象中的方法获得IP地址,如下:/ 获得ip地址
public static String getIpAddr(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()//拿到本地IP地址
}
return ip
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)