基於SpringMVC的攔截器(Interceptor)和過濾器(Filter)的區別與聯繫

  2016/10/07 |  Spring |  admin|  5 條評論 |  1587 viewshtml

一 簡介

(1)過濾器:java

依賴於servlet容器。在實現上基於函數回調,能夠對幾乎全部請求進行過濾,可是缺點是一個過濾器實例只能在容器初始化時調用一次。使用過濾器的目的是用來作一些過濾操做,獲取咱們想要獲取的數據,好比:在過濾器中修改字符編碼;在過濾器中修改HttpServletRequest的一些參數,包括:過濾低俗文字、危險字符等web

關於過濾器的一些用法能夠參考我寫過的這些文章:spring

(2)攔截器:編程

依賴於web框架,在SpringMVC中就是依賴於SpringMVC框架。在實現上基於Java的反射機制,屬於面向切面編程(AOP)的一種運用。因爲攔截器是基於web框架的調用,所以可使用Spring的依賴注入(DI)進行一些業務操做,同時一個攔截器實例在一個controller生命週期以內能夠屢次調用。可是缺點是隻能對controller請求進行攔截,對其餘的一些好比直接訪問靜態資源的請求則沒辦法進行攔截處理mvc

關於過濾器的一些用法能夠參考我寫過的這些文章:app

二 多個過濾器與攔截器的代碼執行順序

若是在一個項目中僅僅只有一個攔截器或者過濾器,那麼我相信相對來講理解起來是比較容易的。可是咱們是否思考過:若是一個項目中有多個攔截器或者過濾器,那麼它們的執行順序應該是什麼樣的?或者再複雜點,一個項目中既有多個攔截器,又有多個過濾器,這時它們的執行順序又是什麼樣的呢?框架

下面我將用簡單的代碼來測試說明:jsp

(1)先定義兩個過濾器:函數

i)過濾器1:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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 TestFilter1 extends OncePerRequestFilter {

 

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)

            throws ServletException, IOException {

        //在DispatcherServlet以前執行

        System.out.println("############TestFilter1 doFilterInternal executed############");

        filterChain.doFilter(request, response);

        //在視圖頁面返回給客戶端以前執行,可是執行順序在Interceptor以後

        System.out.println("############TestFilter1 doFilter after############");

//        try {

//            Thread.sleep(10000);

//        } catch (InterruptedException e) {

//            e.printStackTrace();

//        }

    }

 

}

 

ii)過濾器2:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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中註冊這兩個過濾器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

    <!-- 自定義過濾器:testFilter1 -->    

    <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>

    <!-- 自定義過濾器:testFilter2 -->    

    <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,基本攔截器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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{

    

    /**

     * 在DispatcherServlet以前執行

     * */

    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {

        System.out.println("************BaseInterceptor preHandle executed**********");

        return true;

    }

 

    /**

     * 在controller執行以後的DispatcherServlet以後執行

     * */

    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**********");

//        Thread.sleep(10000);

    }

 

}

ii)指定controller請求的攔截器:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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的配置文件中註冊這兩個攔截器:

1

2

3

4

5

6

7

8

9

10

11

12

    <!-- 攔截器 -->

    <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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<%@ 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="<%=basePath%>">

<title>FilterDemo</title>

</head>

<body>

    <%

        System.out.println("test.jsp is loading");

    %>

    <div align="center">

        This is test page

    </div>

</body>

</html>

(5)測試效果:

啓動此測試項目,能夠看到控制檯中輸出以下:

20161007005001

這就說明了過濾器的運行是依賴於servlet容器的,跟springmvc等框架並無關係。而且,多個過濾器的執行順序跟xml文件中定義的前後關係有關

接着清空控制檯中的輸出內容並訪問:http://localhost:9180/FilterDemo/test.html

能夠看到,此時的控制檯輸出結果以下:

20161007005244

相信從這個打印輸出,你們就能夠很清晰地看到有多個攔截器和過濾器存在時的整個執行順序了。固然,對於過個攔截器它們之間的執行順序跟在SpringMVC的配置文件中定義的前後順序有關

注:對於整個SpringMVC的執行流程來講,若是加上上面的攔截器和過濾器,其最終的執行流程就以下圖所示:

dddd

PS:這個圖是我用畫圖軟件畫出來的,將就着看吧^_^

參考:

  • 《Spring實戰(第4版)》第5章
相關文章
相關標籤/搜索