Filter的过滤链理解

一、Filter过滤链

   web.xml配置了filter过滤器,在容器启动的时候执行了init()方法进行了初始化,然后在容器关闭的时候执行了destroy()方法销毁过滤器,在每次服务器接受请求的时候每次都会先过一遍过滤器,如果有合适的过滤器就会执行相应过滤器的doFilter方法。

   doFilter方法有3个参数 ServletRequest、ServletResponse、FilterChain;前两个分别是请求和返回对象,为的是过滤后还能够进行请求或转发。FilterChain是一个过滤链,他包含了相同过滤条件的所有过滤器,例如:

  Filter的过滤链理解

  类似这种相同匹配模式的过滤器会存在于同一个过滤链中,然后按照初始化的先后顺一次排列,实现逐层过滤。其中filterChain中有个doFilter方法,他的作用是将当前请求转发给过滤链中的下一个过滤器进行过滤,然后将过滤结果,只有等待下一个过滤器执行过滤完成后才能继续执行。该执行过程类似如下图:

  Filter的过滤链理解

  如上图,通过过滤链逐层执行过滤就像一层嵌套,一层套一层,如果过滤链中只有一个过滤器(或者执行到最后一个)的话,执行了chain.doFilter()他会直接将请求转发出去,获取request resource资源,因为从始至终都是同一个request和response在传递,所以每次过滤都可以修改请求或返回结果,实现了过滤修改的目的。

  代码实例:(只贴主要代码)

    ResponseFilter.java

 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
CustomResponseWrapper customResponseWrapper = new CustomResponseWrapper((HttpServletResponse) servletResponse);
System.out.println("ResponseFilter 执行前");
filterChain.doFilter(servletRequest,customResponseWrapper);//执行下一层过滤
System.out.println("ResponseFilter 执行后");
}
}

   SecondFilter.java

 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Second doFilter执行前");
filterChain.doFilter(servletRequest,servletResponse);//这是最后一层过滤器,会直接请求resource
System.out.println("Second doFilter执行后");
}

  web.xml (两个过滤器使用了相同的匹配模式‘/*’,所以会处于同一过滤链中)

 <filter>
<filter-name>ResponseFilter</filter-name>
<filter-class>filter.ResponseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ResponseFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SecondFilter</filter-name>
<filter-class>filter.SecondFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecondFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  执行结果:

  Filter的过滤链理解

  从执行结果并结合以上分析可以很清楚的看出doFilter的执行顺序

二、通过Filter过滤器实现对response内容的修改

  CustomPrintWriter.java (重写PrintWriter方法)

 public class CustomPrintWriter extends PrintWriter{
private StringBuilder buffer; public CustomPrintWriter(Writer out) {
super(out);
buffer = new StringBuilder();
} @Override
public void write(char[] buf, int off, int len) {
char[] dest = new char[len];
System.arraycopy(buf,off,dest,0,len);//深复制字符数组
buffer.append(dest);
} public String getContent() {
return buffer.toString();
}
}

  CustomResponseWrapper.java (重写HttpServletResponseWrapper方法)

 public class CustomResponseWrapper extends HttpServletResponseWrapper{
private CustomPrintWriter customPrintWriter; public CustomResponseWrapper(HttpServletResponse response) {
super(response);
} @Override
public PrintWriter getWriter() throws IOException {
customPrintWriter = new CustomPrintWriter(super.getWriter());
return customPrintWriter;
} public CustomPrintWriter getCustomPrintWriter() {
return customPrintWriter;
}
}

  ResponseFilter.java (过滤器)

 public class ResponseFilter implements Filter{
public void init(FilterConfig filterConfig) throws ServletException {} public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
CustomResponseWrapper customResponseWrapper = new CustomResponseWrapper((HttpServletResponse) servletResponse);
filterChain.doFilter(servletRequest,customResponseWrapper);//转发请求获取请求返回结果
CustomPrintWriter writer = customResponseWrapper.getCustomPrintWriter();//获取请求的返回结果
if(writer != null){
String content = writer.getContent();
/**
* 在不修改jsp源码的情况下修改展示内容
*/
content = content.replace("XXX", LoginCheckServlet.username);
servletResponse.getWriter().write(content);
}
} public void destroy() {}
}

  

  

上一篇:Python pyspider 安装与开发


下一篇:Xamarin 自定义 ToolbarItem 溢出菜单实现(Popover/Popup) 弹出下拉效果