一 简介
(1)过滤器:
依赖于servlet容器。在实现上基于函数回调,可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次。使用过滤器的目的是用来做一些过滤操作,获取我们想要获取的数据,比如:在过滤器中修改字符编码;在过滤器中修改HttpServletRequest的一些参数,包括:过滤低俗文字、危险字符等
关于过滤器的一些用法可以参考我写过的这些文章:
(2)拦截器:
依赖于web框架,在SpringMVC中就是依赖于SpringMVC框架。在实现上基于Java的反射机制,属于面向切面编程(AOP)的一种运用。由于拦截器是基于web框架的调用,因此可以使用Spring的依赖注入(DI)进行一些业务操作,同时一个拦截器实例在一个controller生命周期之内可以多次调用。但是缺点是只能对controller请求进行拦截,对其他的一些比如直接访问静态资源的请求则没办法进行拦截处理
关于过滤器的一些用法可以参考我写过的这些文章:
二 多个过滤器与拦截器的代码执行顺序
如果在一个项目中仅仅只有一个拦截器或者过滤器,那么我相信相对来说理解起来是比较容易的。但是我们是否思考过:如果一个项目中有多个拦截器或者过滤器,那么它们的执行顺序应该是什么样的?或者再复杂点,一个项目中既有多个拦截器,又有多个过滤器,这时它们的执行顺序又是什么样的呢?
下面我将用简单的代码来测试说明:
(1)先定义两个过滤器:
i)过滤器1:
- <a target="_blank" href="http://www.07net01.com/tags-package-0.html" class="infotextkey" style="background:transparent; color:rgb(66,139,202)">package</a> cn.zifangsky.filter;
-
- import java.io.IOException;
-
- import javax.servlet.FilterChain;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.filter.OncePerRequestFilter;
-
- public class TestFilter1 extends OncePerRequestFilter {
-
- protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
-
- <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############");
- filterChain.doFilter(request, response);
-
- System.out.println("############TestFilter1 doFilter after############");
- }
-
- }
ii)过滤器2:
- package cn.zifangsky.filter;
-
- import java.io.IOException;
-
- import javax.servlet.FilterChain;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.filter.OncePerRequestFilter;
-
- public class TestFilter2 extends OncePerRequestFilter {
-
- protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
- System.out.println("############TestFilter2 doFilterInternal executed############");
- filterChain.doFilter(request, response);
- System.out.println("############TestFilter2 doFilter after############");
-
- }
-
- }
iii)在web.xml中注册这两个过滤器:
-
- <filter>
- <filter-name>testFilter1</filter-name>
- <filter-class>cn.zifangsky.filter.TestFilter1</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>testFilter1</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <filter>
- <filter-name>testFilter2</filter-name>
- <filter-class>cn.zifangsky.filter.TestFilter2</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>testFilter2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
(2)再定义两个拦截器:
i)拦截器1,基本拦截器:
- package cn.zifangsky.interceptor;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- public class BaseInterceptor implements HandlerInterceptor{
-
-
- public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
- System.out.println("************BaseInterceptor preHandle executed**********");
- return true;
- }
-
-
- public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
- throws Exception {
- System.out.println("************BaseInterceptor postHandle executed**********");
- }
-
-
- public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
- throws Exception {
- System.out.println("************BaseInterceptor afterCompletion executed**********");
- }
-
- }
ii)指定controller请求的拦截器:
- package cn.zifangsky.interceptor;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- public class TestInterceptor implements HandlerInterceptor {
-
- public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
- System.out.println("************TestInterceptor preHandle executed**********");
- return true;
- }
-
- public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
- throws Exception {
- System.out.println("************TestInterceptor postHandle executed**********");
- }
-
- public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
- throws Exception {
- System.out.println("************TestInterceptor afterCompletion executed**********");
- }
- }
iii)在SpringMVC的配置文件中注册这两个拦截器:
- <!-- 拦截器 -->
- nbsp; <mvc:interceptors>
- <!-- 对所有请求都拦截,公共拦截器可以有多个 -->
- <bean name="baseInterceptor" class="cn.zifangsky.interceptor.BaseInterceptor" />
- <!-- <bean name="testInterceptor" class="cn.zifangsky.interceptor.TestInterceptor" /> -->
- <mvc:interceptor>
- <!-- 对/test.html进行拦截 -->
- <mvc:mapping path="/test.html"/>
- <!-- 特定请求的拦截器只能有一个 -->
- <bean class="cn.zifangsky.interceptor.TestInterceptor" />
- </mvc:interceptor>
- </mvc:interceptors>
(3)定义一个测试使用的controller:
- package cn.zifangsky.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- @Controller
- public class TestController {
-
- @RequestMapping("/test.html")
- public ModelAndView handleRequest(){
- System.out.println("---------TestController executed--------");
- return new ModelAndView("test");
- }
- }
(4)视图页面test.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <base href="http://983836259.blog.51cto.com/7311475/">
- <title>FilterDemo</title>
- </head>
- <body>
- <%
- System.out.println("test.jsp is loading");
- %>
- <div align="center">
- This is test page
- </div>
- </body>
- </html>
(5)测试效果:
启动此测试项目,可以看到控制台中输出如下:
这就说明了过滤器的运行是依赖于servlet容器的,跟springmvc等框架并没有关系。并且,多个过滤器的执行顺序跟xml文件中定义的先后关系有关
接着清空控制台中的输出内容并访问:http://localhost:9180/FilterDemo/test.html
可以看到,此时的控制台输出结果如下:
相信从这个打印输出,大家就可以很清晰地看到有多个拦截器和过滤器存在时的整个执行顺序了。当然,对于过个拦截器它们之间的执行顺序跟在SpringMVC的配置文件中定义的先后顺序有关
注:对于整个SpringMVC的执行流程来说,如果加上上面的拦截器和过滤器,其最终的执行流程就如下图所示: