一 簡介
(1)過濾器:java
依賴於servlet容器。在實現上基於函數回調,能夠對幾乎全部請求進行過濾,可是缺點是一個過濾器實例只能在容器初始化時調用一次。使用過濾器的目的是用來作一些過濾操做,獲取咱們想要獲取的數據,好比:在過濾器中修改字符編碼;在過濾器中修改HttpServletRequest的一些參數,包括:過濾低俗文字、危險字符等程序員
關於過濾器的一些用法能夠參考我寫過的這些文章:web
(2)攔截器:cookie
依賴於web框架,在SpringMVC中就是依賴於SpringMVC框架。在實現上基於Java的反射機制,屬於面向切面編程(AOP)的一種運用。因爲攔截器是基於web框架的調用,所以可使用Spring的依賴注入(DI)進行一些業務操做,同時一個攔截器實例在一個controller生命週期以內能夠屢次調用。可是缺點是隻能對controller請求進行攔截,對其餘的一些好比直接訪問靜態資源的請求則沒辦法進行攔截處理session
關於過濾器的一些用法能夠參考我寫過的這些文章:mvc
二 多個過濾器與攔截器的代碼執行順序
若是在一個項目中僅僅只有一個攔截器或者過濾器,那麼我相信相對來講理解起來是比較容易的。可是咱們是否思考過:若是一個項目中有多個攔截器或者過濾器,那麼它們的執行順序應該是什麼樣的?或者再複雜點,一個項目中既有多個攔截器,又有多個過濾器,這時它們的執行順序又是什麼樣的呢?
下面我將用簡單的代碼來測試說明:
(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)測試效果:
啓動此測試項目,能夠看到控制檯中輸出以下:
![20161007005012337.png wKioL1hHhYrRCpQYAABafsYR7go378.png](http://static.javashuo.com/static/loading.gif)
這就說明了過濾器的運行是依賴於servlet容器的,跟springmvc等框架並無關係。而且,多個過濾器的執行順序跟xml文件中定義的前後關係有關
接着清空控制檯中的輸出內容並訪問:http://localhost:9180/FilterDemo/test.html
能夠看到,此時的控制檯輸出結果以下:
![20161007005257493.png wKiom1hHhaPRQuBxAACG4WdOJbM758.png](http://static.javashuo.com/static/loading.gif)
相信從這個打印輸出,你們就能夠很清晰地看到有多個攔截器和過濾器存在時的整個執行順序了。固然,對於過個攔截器它們之間的執行順序跟在SpringMVC的配置文件中定義的前後順序有關
注:對於整個SpringMVC的執行流程來講,若是加上上面的攔截器和過濾器,其最終的執行流程就以下圖所示:
![20161007014703521.png wKiom1hHhbmxseDtAACidU9Y84s787.png](http://static.javashuo.com/static/loading.gif)