先贴代码,用输出结果来阐述原理。
web.xml
代码完毕,然后刷新任意Context下的任意页面,输出如下:
** 执行doFilter()方法之前. **
** 过滤器开始工作 **
** 执行SimpleFilter2.doFilter()方法之前. **
** SimpleFilter2.过滤器开始工作 **
** 执行SimpleFilter2.doFilter()方法之后. **
** 执行doFilter()方法之后. **
这时候想想链式处理,每个过滤器都会输出自己的doFilter()方法处理前后。而且最早声明的过滤器会包围最末声明的过滤器。这就是链式的处理特点!
使用ServletFilter的例子有:登录验证,设置Request.setCharacterEncoding编码
package org.lxh.filterdemo;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SimpleFilter implements Filter{
public void init(FilterConfig config) throws ServletException{
String initParam = config.getInitParameter("ref");
System.out.println("** 过滤器初始化,初始化参数="+initParam+" **");
}
public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)throws IOException,ServletException{
System.out.println("** 执行doFilter()方法之前. **");
System.out.println("** 过滤器开始工作 **");
chain.doFilter(req, resp);
System.out.println("** 执行doFilter()方法之后. **");
}
public void destroy(){
System.out.println("** 过滤器销毁 **");
}
}
package org.lxh.filterdemo;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SimpleFilter2 implements Filter{
public void init(FilterConfig config) throws ServletException{
String initParam = config.getInitParameter("ref");
System.out.println("** SimpleFilter2.过滤器初始化,初始化参数="+initParam+" **");
}
public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)throws IOException,ServletException{
System.out.println("** 执行SimpleFilter2.doFilter()方法之前. **");
System.out.println("** SimpleFilter2.过滤器开始工作 **");
chain.doFilter(req, resp);
System.out.println("** 执行SimpleFilter2.doFilter()方法之后. **");
}
public void destroy(){
System.out.println("** SimpleFilter2.过滤器销毁 **");
}
}
web.xml
<filter>
<filter-name>simple</filter-name>
<filter-class>org.lxh.filterdemo.SimpleFilter</filter-class>
<init-param>
<param-name>ref</param-name>
<param-value>HELLO WORLD</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>simple</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>simple2</filter-name>
<filter-class>org.lxh.filterdemo.SimpleFilter2</filter-class>
<init-param>
<param-name>ref</param-name>
<param-value>HELLO WORLD</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>simple2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
代码完毕,然后刷新任意Context下的任意页面,输出如下:
** 执行doFilter()方法之前. **
** 过滤器开始工作 **
** 执行SimpleFilter2.doFilter()方法之前. **
** SimpleFilter2.过滤器开始工作 **
** 执行SimpleFilter2.doFilter()方法之后. **
** 执行doFilter()方法之后. **
这时候想想链式处理,每个过滤器都会输出自己的doFilter()方法处理前后。而且最早声明的过滤器会包围最末声明的过滤器。这就是链式的处理特点!
使用ServletFilter的例子有:登录验证,设置Request.setCharacterEncoding编码