java filter 如何中获取请求地址?
当我们访问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 {
}
}
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain){
HttpServletRequest request=(HttpServletRequest)req;
String uri=request.getRequestURI(); //uri就是获取到的连接地址!
}
}
//谢谢采纳
2015-03-10
广告 您可能关注的内容 |