java中filter怎么获取a标签请求的路径
1个回答
展开全部
当我们访问index.jsp/时,其实并不是我们浏览器真正访问到了服务器上的index.jsp 文件,而是先由代理服务器去访问index.jsp。
代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问index.jsp的。
所以index.jsp中通过 request.getRemoteAddr()的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。
于是可得出获得客户端真实IP地址 的方法:
public class Myfilter implements Filter {
public void destroy() {
}
private HttpServletRequest request;
private HttpServletResponse response;
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
request = (HttpServletRequest) arg0;
response = (HttpServletResponse) arg1;
System.out.println(request.getRequestURI());
System.out.println("===ff===="+request.getQueryString());
if("1".equals(request.getRequestURI().substring(("/"+request.getContextPath()).length()))){
request.getRequestDispatcher("/my.jsp").forward(request, response);
return;
}
arg2.doFilter(request, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问index.jsp的。
所以index.jsp中通过 request.getRemoteAddr()的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。
于是可得出获得客户端真实IP地址 的方法:
public class Myfilter implements Filter {
public void destroy() {
}
private HttpServletRequest request;
private HttpServletResponse response;
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
request = (HttpServletRequest) arg0;
response = (HttpServletResponse) arg1;
System.out.println(request.getRequestURI());
System.out.println("===ff===="+request.getQueryString());
if("1".equals(request.getRequestURI().substring(("/"+request.getContextPath()).length()))){
request.getRequestDispatcher("/my.jsp").forward(request, response);
return;
}
arg2.doFilter(request, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询