filter过滤器和interceptor拦截器的区别和执行顺序

1.过滤器

      过滤器是一个程序,它先于与之相关的servlet或JSP页面运行在服务器上。它是随你的web应用启动而启动的,只初始化一次,以后就可以拦截相关请求,只有当你的web应用停止或重新部署的时候才销毁。
作用
      请求和回应的过滤,传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者struts的action前统一设置字符集,或者去除掉一些非法字符(聊天室经常用到的,一些骂人的话)。

Servlet过滤器的基本原理
      在请求进入容器之后,还未进入Servlet之前进行预处理;在请求结束返回给前端之前进行后期处理。处理完成后,它会交给下一个过滤器处理,直到请求发送到目标为止。

2.拦截器

      拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。比如日志,安全等。
      拦截器链,就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。
一般拦截器方法都是通过动态代理的方式实现。

作用
比如通过它来进行权限验证,或者判断用户是否登陆,或者是像12306 判断当前时间是否是购票时间。

3.区别

      ①拦截器是基于动态代理的,属于面向切面编程(AOP)的一种运用;而过滤器是基于函数回调。
  ②拦截器不依赖于servlet容器,通过动态代理实现;过滤器依赖于servlet容器。
  ③拦截器可以在方法前后,异常前后等多次调用,而过滤器只能在某一次请求前和请求后各调用一次。
      ④过滤器几乎可以过滤所有的请求,包括页面、图片、js等静态资源以及请求;而拦截器只能对一些action或者controller进行拦截。
  ⑤拦截器可以利用依赖注入,而过滤器则不能直接注入(
原因详情见:http://blog.csdn.net/qq_39470733)因此在Spring框架程序中,优先拦截器。

4.如果在一个项目中仅仅只有一个拦截器或者过滤器,那么我相信相对来说理解起来是比较容易的。但是我们是否思考过:如果一个项目中有多个拦截器或者过滤器,那么它们的执行顺序应该是什么样的?或者再复杂点,一个项目中既有多个拦截器,又有多个过滤器,这时它们的执行顺序又是什么样的呢?

(1)先定义两个过滤器:
①filter过滤器 1
    
  1. public class TestFilter1 extends OncePerRequestFilter {  
  2.   
  3.     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
  4.             throws ServletException, IOException {  
  5.         //在DispatcherServlet之前执行  
  6.         <a target="_blank" href="http://www.07net01.com/tags-system-0.html" class="infotextkey" style="background:transparent; color:rgb(66,139,202)">system</a>.out.println("############TestFilter1 doFilterInternal executed############");  
  7.         filterChain.doFilter(request, response);  
  8.         //在视图页面返回给<a target="_blank" href="http://www.07net01.com/tags-%E5%AE%A2%E6%88%B7%E7%AB%AF-0.html" class="infotextkey" style="background:transparent; color:rgb(66,139,202)">客户端</a>之前执行,但是执行顺序在Interceptor之后  
  9.         System.out.println("############TestFilter1 doFilter after############");  
  10.     } 

②filter过滤器 2

  1. public class TestFilter2 extends OncePerRequestFilter {  
  2.   
  3.     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
  4.             throws ServletException, IOException {  
  5.         System.out.println("############TestFilter2 doFilterInternal executed############");  
  6.         filterChain.doFilter(request, response);  
  7.         System.out.println("############TestFilter2 doFilter after############");  
  8.     }    

在web.xml中注册这两个过滤器

    <!-- 自定义过滤器:testFilter1 -->   

    1.    <filter>  
    2.         <filter-name>testFilter1</filter-name>  
    3.         <filter-class>cn.zifangsky.filter.TestFilter1</filter-class>  
    4.     </filter>  
    5.     <filter-mapping>  
    6.         <filter-name>testFilter1</filter-name>  
    7.         <url-pattern>/*</url-pattern>  
    8.     </filter-mapping>  
    9.     <!-- 自定义过滤器:testFilter2 -->   
    10.    <filter>  
    11.         <filter-name>testFilter2</filter-name>  
    12.         <filter-class>cn.zifangsky.filter.TestFilter2</filter-class>  
    13.     </filter>  
    14.     <filter-mapping>  
    15.         <filter-name>testFilter2</filter-name>  
    16.         <url-pattern>/*</url-pattern>  
    17.     </filter-mapping>

(2)再定义两个拦截器:

①拦截器 1 

  1. public class BaseInterceptor implements HandlerInterceptor{      
  2.     /** 
  3.      * 在DispatcherServlet之前执行 
  4.      * */  
  5.     public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {  
  6.         System.out.println("************BaseInterceptor preHandle executed**********");  
  7.         return true;  
  8.     }  

  9.     /** 
  10.      * 在controller执行之后的DispatcherServlet之后执行 
  11.      * */  
  12.     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)  
  13.             throws Exception {  
  14.         System.out.println("************BaseInterceptor postHandle executed**********");  
  15.     }  
  16.       
  17.     /** 
  18.      * 在页面渲染完成返回给客户端之前执行 
  19.      * */  
  20.     public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)  
  21.             throws Exception {  
  22.         System.out.println("************BaseInterceptor afterCompletion executed**********");  
  23. //      Thread.sleep(10000);  
  24.     }  
  25. }

②拦截器 2

  1. public class TestInterceptor implements HandlerInterceptor {  
  2.   
  3.     public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {  
  4.         System.out.println("************TestInterceptor preHandle executed**********");  
  5.         return true;  
  6.     }  
  7.   
  8.     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)  
  9.             throws Exception {  
  10.         System.out.println("************TestInterceptor postHandle executed**********");  
  11.     }  
  12.   
  13.     public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)  
  14.             throws Exception {  
  15.         System.out.println("************TestInterceptor afterCompletion executed**********");  
  16.     }  
  17. }  

③在SpringMVC配置文件中配置拦截器

  1. <!-- 拦截器 -->  
  2. nbsp;   <mvc:interceptors>  
  3.     <!-- 对所有请求都拦截,公共拦截器可以有多个 -->  
  4.     <bean name="baseInterceptor" class="cn.zifangsky.interceptor.BaseInterceptor" />  
  5.     <!-- <bean name="testInterceptor" class="cn.zifangsky.interceptor.TestInterceptor" /> -->  
  6.     <mvc:interceptor>       
  7.         <!-- 对/test.html进行拦截 -->  
  8.         <mvc:mapping path="/test.html"/>  
  9.         <!-- 特定请求的拦截器只能有一个 -->  
  10.         <bean class="cn.zifangsky.interceptor.TestInterceptor" />  
  11.     </mvc:interceptor>  
  12. </mvc:interceptors>  
(3)定义一个测试controller:

  1. @Controller  
  2. public class TestController {  
  3.       
  4.     @RequestMapping("/test.html")  
  5.     public ModelAndView handleRequest(){  
  6.         System.out.println("---------TestController executed--------");  
  7.         return new ModelAndView("test");  
  8.     }  
(4)定义一个测试页面:

  1. <body>  
  2.     <%  
  3.         System.out.println("test.jsp is loading");  
  4.     %>  
  5.     <div align="center">  
  6.         This is test page  
  7.     </div>  
  8. </body> 

(5)测试效果:

①单独访问filter的测试请求  http://localhost:8080/FilterDemo


这就说明过滤器的运行是依赖于servlet容器的,跟springmvc等框架并没有关系。并且,多个过滤器的执行顺序跟xml文件中定义的先后关系有关

②接着清空控制台中的输出内容并访问:http://localhost:8080/FilterDemo/test.html


相信从这个打印输出,大家就可以很清晰地看到有多个拦截器和过滤器存在时的整个执行顺序了。当然, 对于过个拦截器它们之间的执行顺序跟在SpringMVC的配置文件中定义的先后顺序有关

如有其它更好建议或者不同意见欢迎留言或者入群探讨:


猜你喜欢

转载自blog.csdn.net/qq_39470733/article/details/79383901