发布网友 发布时间:2022-04-24 17:34
共1个回答
热心网友 时间:2023-10-26 20:28
package A;
import javax.servlet.http.HttpServletRequest;
public class a {
public String getRemortIP(HttpServletRequest request)
{
if (request.getHeader("x-forwarded-for") == null)
{
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
public 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();
}
return ip;
}
}
接着有些了个jsp来调用:
<%@ page language="java" import="java.util.*,A.*" pageEncoding="utf-8"%>
<jsp:useBean id="test" class="A.a" scope="request" > </jsp:useBean>
<%
out.println("request.getRemoteAddr(): " + request.getRemoteAddr()); //这里输出的不是真正的ip
test.getIpAddr(); //这里的参数怎么写?
%>