日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring mvc DispatcherServlet详解之interceptor和filter的区别

發布時間:2025/4/5 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring mvc DispatcherServlet详解之interceptor和filter的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先我們看一下spring mvc Interceptor的功能及實現:

http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy_IPeumazg0ppnbmwqFUtLp9kSpuPPpeysf6EnHBLYFeWrbjqMq8BlWKQz_7MSDhGQTVl32fpxCMm

SpringMVC 中的Interceptor 攔截器也是相當重要和相當有用的,它的主要作用是攔截用戶的請求并進行相應的處理,其他的作用比如通過它來進行權限驗證,或者是來判斷用戶是否登陸,日志記錄,或者限制時間點訪問。

SpringMVC 中的Interceptor 攔截請求是通過HandlerInterceptor 來實現的。在SpringMVC 中定義一個Interceptor 非常簡單,主要有兩種方式,第一種方式是要定義的Interceptor類要實現了Spring 的HandlerInterceptor 接口,或者是這個類繼承實現了HandlerInterceptor 接口的類,比如Spring 已經提供的實現了HandlerInterceptor 接口的抽象類HandlerInterceptorAdapter ;第二種方式是實現Spring的WebRequestInterceptor接口,或者是繼承實現了WebRequestInterceptor的類。

HandlerInterceptor 接口中定義了三個方法,我們就是通過這三個方法來對用戶的請求進行攔截處理的。

?? (1 )preHandle (HttpServletRequest request, HttpServletResponse response, Object handle) 方法。該方法將在請求處理之前進行調用。SpringMVC 中的Interceptor 是鏈式的調用的,在一個應用中或者說是在一個請求中可以同時存在多個Interceptor 。每個Interceptor 的調用會依據它的聲明順序依次執行,而且最先執行的都是Interceptor 中的preHandle 方法,所以可以在這個方法中進行一些前置初始化操作或者是對當前請求的一個預處理,也可以在這個方法中進行一些判斷來決定請求是否要繼續進行下去。該方法的返回值是布爾值Boolean 類型的,當它返回為false 時,表示請求結束,后續的Interceptor 和Controller 都不會再執行;當返回值為true 時就會繼續調用下一個Interceptor 的preHandle 方法,如果已經是最后一個Interceptor 的時候就會是調用當前請求的Controller 方法。

?? (2 )postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView) 方法,由preHandle 方法的解釋我們知道這個方法包括后面要說到的afterCompletion 方法都只能是在當前所屬的Interceptor 的preHandle 方法的返回值為true 時才能被調用。postHandle 方法,顧名思義就是在當前請求進行處理之后,也就是Controller 方法調用之后執行,但是它會在DispatcherServlet 進行視圖返回渲染之前被調用,所以我們可以在這個方法中對Controller 處理之后的ModelAndView 對象進行操作。postHandle 方法被調用的方向跟preHandle 是相反的,也就是說先聲明的Interceptor 的postHandle 方法反而會后執行,這和Struts2 里面的Interceptor 的執行過程有點類型。Struts2 里面的Interceptor 的執行過程也是鏈式的,只是在Struts2 里面需要手動調用ActionInvocation 的invoke 方法來觸發對下一個Interceptor 或者是Action 的調用,然后每一個Interceptor 中在invoke 方法調用之前的內容都是按照聲明順序執行的,而invoke 方法之后的內容就是反向的。

?? (3 )afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex) 方法,該方法也是需要當前對應的Interceptor 的preHandle 方法的返回值為true 時才會執行。顧名思義,該方法將在整個請求結束之后,也就是在DispatcherServlet 渲染了對應的視圖之后執行。這個方法的主要作用是用于進行資源清理工作的。 我們的系統日志的攔截在這個方法中,可以記錄日志的相關的參數,檢測方法的執行。

?

在這里,我們就有一個疑問了:攔截器和過濾器到底有什么不同呢?

首先,我們看一下官方是怎么解釋的:

public interface HandlerInterceptor Workflow interface that allows for customized handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation. A HandlerInterceptor gets called before the appropriate HandlerAdapter triggers the execution of the handler itself. This mechanism can be used for a large field of preprocessing aspects, e.g. for authorization checks, or common handler behavior like locale or theme changes. Its main purpose is to allow for factoring out repetitive handler code.In an async processing scenario, the handler may be executed in a separate thread while the main thread exits without rendering or invoking the postHandle and afterCompletion callbacks. When concurrent handler execution completes, the request is dispatched back in order to proceed with rendering the model and all methods of this contract are invoked again. For further options and details see org.springframework.web.servlet.AsyncHandlerInterceptorTypically an interceptor chain is defined per HandlerMapping bean, sharing its granularity. To be able to apply a certain interceptor chain to a group of handlers, one needs to map the desired handlers via one HandlerMapping bean. The interceptors themselves are defined as beans in the application context, referenced by the mapping bean definition via its "interceptors" property (in XML: a <list> of <ref>).HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context.As a basic guideline, fine-grained handler-related preprocessing tasks are candidates for HandlerInterceptor implementations, especially factored-out common handler code and authorization checks. On the other hand, a Filter is well-suited for request content and view content handling, like multipart forms and GZIP compression. This typically shows when one needs to map the filter to certain content types (e.g. images), or to all requests.

?

public interface Filter A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.Filters are configured in the deployment descriptor of a web applicationExamples that have been identified for this design are 1) Authentication Filters 2) Logging and Auditing Filters 3) Image conversion Filters 4) Data compression Filters 5) Encryption Filters 6) Tokenizing Filters 7) Filters that trigger resource access events 8) XSL/T filters 9) Mime-type chain Filter

interceptor 和filter的概念相似,但主要不同點有:

web應用的過濾請求,僅使用web應用;

interceptor應用于特定組別的handler,可以web應用也可以企業應用;

從google找到的資料:http://www.linkedin.com/groups/what-is-difference-between-interceptor-3983267.S.5844715100472107010

Filter is used only in web applications whereas interceptor can be used with web as well as enterprise applications. Life cycle methods of both, also differs. The Interceptor stack fires on requests in a configured package while filters only apply to their mapped URL's.?

Example:?

A Servlet Filter is used in the web layer only, you can't use it outside of a?
web context. Interceptors can be used anywhere.?

The interceptor stack fires on every request.?
Filters only apply to the urls for which they are defined.?

Filters can be used when you want to modify any request or response parameters like headers. For example you would like to add a response header "Powered By Surya" to each generated response. Instead of adding this header in each resource method you would use a response filter to add this header.?

There are filters on the server side and the client side.?

In Summary:?

Filters:?

(1)Based on Servlet Specification?
(2)Executes on the pattern matches on the request.?
(3) Not configurable method calls.?


Interceptors:?
(1)Based on Struts2.?
(2)Executes for all the request qualifies for a front controller( A Servlet filter ).And can be configured to execute additional interceptor for a particular action execution.?
(3)Methods in the Interceptors can be configured whether to execute or not by means of excludemethods or includeMethods

?

轉載于:https://www.cnblogs.com/davidwang456/p/4108355.html

總結

以上是生活随笔為你收集整理的spring mvc DispatcherServlet详解之interceptor和filter的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 91av在线免费视频 | 日韩精品视频在线播放 | www av| ass东方小嫩模pics | 欧美成年人在线观看 | 无遮挡aaaaa大片免费看 | 一边顶弄一边接吻 | 免费在线观看黄网站 | 韩国bj大尺度vip福利网站 | 91快射| 亚洲精选一区二区三区 | 亚洲欧洲日韩 | 国产视频xxxx | 懂色中文一区二区在线播放 | 日本一二三区视频 | 中文字幕理伦片免费看 | 国产盗摄一区二区 | 黑人操日本女人 | 亚洲日本久久 | 日韩成人在线免费观看 | 成人免费黄色小视频 | 自拍视频第一页 | 国产色秀视频 | 兔费看少妇性l交大片免费 日韩高清不卡 | 中文字幕女同女同女同 | 中文在线最新版天堂 | 午夜免费福利视频 | 成人午夜淫片免费观看 | 免费污片软件 | 又大又粗欧美黑人aaaaa片 | 一区二区高潮 | 爱情岛av永久入口 | 国产麻豆久久 | 一级淫片免费看 | 国产人妖在线观看 | 91精品在线观看入口 | 女同中文字幕 | 妓院一钑片免看黄大片 | 日日摸夜夜添夜夜添高潮喷水 | tube极品少妇videos | sm在线观看 | 特黄特色大片免费视频大全 | 国产富婆一区二区三区 | 亚洲最新在线观看 | 在线视频福利 | 91桃色在线观看 | 综合色区| 成年人免费网站在线观看 | 日韩av综合网 | 午夜国产在线视频 | 一级免费大片 | 三度诱惑免费版电影在线观看 | 久久久一级黄色片 | 欧美少妇毛茸茸 | 久久免费视频观看 | 日本a√在线观看 | 久久亚洲av无码精品色午夜麻豆 | 国产精品一区视频 | 中文字字幕在线中文乱码 | 久久激情网 | www.色就是色 | 欧美黄色a级大片 | 亚洲色图激情小说 | a级大片免费看 | 成人午夜视频免费 | 四虎永久免费在线观看 | 波多野吉衣视频在线观看 | aaa在线视频 | 午夜婷婷在线观看 | 成人高清在线观看 | 色婷婷www| 美女操操操 | 波多野结衣 一区 | 亚洲小说区图片区 | 深夜久久久 | 精品动漫一区 | 91看片淫黄大片91桃色 | 日韩福利影院 | 亚色中文字幕 | 日韩大片一区二区 | 国产精品又黄又爽又色无遮挡 | 人与动物黄色片 | 一区二区三区四区亚洲 | 一级二级三级黄色片 | 日批av | 视频精品一区 | 黄色片网站免费 | 日本福利片在线观看 | 欧美18—19性高清hd4k | 成人黄色小说在线观看 | 亚洲字幕成人中文在线观看 | 免费的一级黄色片 | 天天躁日日躁狠狠躁 | 很黄很污的视频 | 亚洲第一二区 | 国产视频一区二区在线观看 | 女性裸体瑜伽无遮挡 | 国产精品久久久久久久毛片 | 国产精品美女久久久久久 |