首頁  >  文章  >  Java  >  java中過濾器、監聽器、攔截器機制的實例詳解

java中過濾器、監聽器、攔截器機制的實例詳解

零下一度
零下一度原創
2017-06-25 13:32:382417瀏覽

一、過濾器

Filter也稱之為過濾器,它是Servlet技術中最實用的技術,Web開發人員透過Filter技術,對web伺服器管理的所有web資源:例如Jsp, Servlet, 靜態圖片檔案或靜態html 檔案等進行攔截,從而實現一些特殊的功能。例如實現URL等級的權限存取控制、過濾敏感詞彙、壓縮回應資訊等一些進階功能。

它主要用於對使用者請求進行預處理,也可以對HttpServletResponse進行後處理。使用Filter的完整流程:Filter對使用者請求進行預處理,接著將請求交給Servlet進行處理並產生回應,最後Filter再對伺服器回應進行後處理。

一般與spring架構一起用在以下幾個地方:

1.1 處理字元寫入資料庫編碼問題,在web.xml中配置一下程式碼

 1 <filter> 2       <filter-name>Encoding</filter-name> 3       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4       <init-param> 5           <param-name>encoding</param-name> 6           <param-value>utf-8</param-value> 7       </init-param> 8   </filter> 9   <filter-mapping>10       <filter-name>Encoding</filter-name>11       <url-pattern>/*</url-pattern>12   </filter-mapping>
View Code

1.2 處理與mongodb整合出現的初始化先後載入問題,使用Filter完成代理功能

#
 1 <!-- 告诉ContextLoaderListener叫在spring的配置文档的位置--> 2   <context-param> 
 3      <param-name>contextConfigLocation</param-name> 
 4      <param-value> 
 5          classpath:spring-shiro-web.xml, 6           /WEB-INF/spring-servlet.xml 7      </param-value> 
 8  </context-param> 
 9 10 <filter>11     <filter-name>shiroFilter</filter-name>12     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>13     <init-param>14         <param-name>targetFilterLifecycle</param-name>15         <param-value>true</param-value>16     </init-param>17 </filter>18 <!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->19 <!-- requests.  Usually this filter mapping is defined first (before all others) to -->20 <!-- ensure that Shiro works in subsequent filters in the filter chain:             -->21 <filter-mapping>22     <filter-name>shiroFilter</filter-name>23     <url-pattern>/*</url-pattern>24 </filter-mapping>25 26 <!-- 在tomcat启动的时候优先加载spring的配置文档 -->27     <listener> 
28         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
29      </listener>
View Code
#
过滤器生命周期的四个阶段:
<span style="font-size: 14px">1、实例化:Web容器在部署Web应用程序时对所有过滤器进行实例化。Web容器回调它的无参构造方法。</span><br><span style="font-size: 14px">2、初始化:实例化完成之后,马上进行初始化工作。Web容器回调init()方法。</span>
3、过滤:请求路径匹配过滤器的URL映射时。Web容器回调doFilter()方法——主要的工作方法。
4、销毁: Web容器在卸载Web应用程序前,Web容器回调destroy()方法。

 

二、监听器

监听器Listener就是在application,session,request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件。

Listener是Servlet的监听器,可以监听客户端的请求和服务端的操作等。

主要有以下三类:

1、ServletContext监听

ServletContextListener:用于对Servlet整个上下文进行监听(创建、销毁)。
ServletContextAttributeListener:对Servlet上下文属性的监听(增删改属性)。

2、Session监听

Session属于http协议下的内容,接口位于javax.servlet.http.*包下。

HttpSessionListener接口:对Session的整体状态的监听。
HttpSessionAttributeListener接口:对session的属性监听。
session的销毁有两种情况:

2.1 session超时,web.xml配置: <session-config> <session-timeout>120session-timeout>

session-config>   

2.2 手工使session失效 public void invalidate();//使session失效方法。session.invalidate();

3、Request监听

ServletRequestListener:用于对Request请求进行监听(创建、销毁)。
ServletRequestAttributeListener:对Request属性的监听(增删改属性)。

4、在web.xml中配置

Listener配置信息必须在Filter和Servlet配置之前,Listener的初始化(ServletContentListener初始化)比Servlet和Filter都优先,

而销毁比Servlet和Filter都慢。

<listener> <listener-class>com.listener.classlistener-class> listener>

<context-param> <param-name>contextConfigLocationparam-name>

<param-value>classpath:spring/applicationContext-*.xmlparam-value>

context-param>

< listener>

##   <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>

#listener>

############################################################################################################################################################################################ # ########################################################### 4.1 Spring使用IntrospectorCleanupListener清理快取######這個監聽器的作用是在web應用程式關閉時刷新JDK的JavaBeans的Introspector緩存,以確保Web應用程式的類別載入器以及其載入的類別正確的釋放資源。 ######如果JavaBeans的Introspector已被用來分析應用程式類,系統層級的Introspector快取將持有這些類別的硬引用。因此,這些類別和網路應用程式的類別載入器在網路應用程式關閉時將不會被垃圾收集器回收!而IntrospectorCleanupListener則會對其進行適當的清理,已使其能夠被垃圾收集器回收。 ######唯一能夠清理Introspector的方法是刷新整個Introspector緩存,沒有其他辦法來確切指定應用程式所引用的類別。這將刪除所有其他應用程式在伺服器的快取的Introspector結果。 ######在使用Spring內部的bean機制時,不需要使用此監聽器,因為Spring自己的introspection results cache將會立即刷新被分析過的JavaBeans Introspector cache,而僅僅會在應用程式自己的ClassLoader裡面持有一個cache。雖然Spring本身不產生洩漏,注意,即使在Spring框架的類別本身駐留在一個「共同」類別載入器(如係統的ClassLoader)的情況下,也仍然應該使用IntrospectorCleanupListener。在這種情況下,這個IntrospectorCleanupListener將會妥善清理Spring的introspection cache。 ######應用程式類,幾乎不需要直接使用JavaBeans Introspector,所以,通常都不是Introspector resource造成記憶體外洩。相反,許多函式庫和框架,不清理Introspector,例如: Struts和Quartz。 ######要注意的是一個簡單Introspector洩漏將會導致整個Web應用程式的類別載入器不會被回收!這樣做的結果,將會是在web應用程式關閉時,該應用程式所有的靜態類別資源(例如:單一實例物件)都沒有被釋放。而導致記憶體外洩的根本原因其實並不是這些未回收的類別! ######注意:IntrospectorCleanupListener應該註冊為web.xml中的第一個Listener,在任何其他Listener之前註冊,例如在Spring's ContextLoaderListener註冊之前,才能確保IntrospectorCleanupListener在Web應用的生命週期的適當時機生效。 ###################### ###<###listener>################ ################   ###<###listener-class>org.springframework.web.util.IntrospectorCleanupListener### #########################

listener>

#三、spring攔截器











spring 管理的攔截器需要繼承


### #####################Intercepter implements HandlerInterceptor############################################################################################### ###################################要進入攔截器優先需要配置spring的入口###### ##############################     spring#### org.springframework.web.servlet.DispatcherServlet###  ###  ###     /###  ###### 以上這種寫法不會過濾掉.jsp結尾的url ,若需要處理.jsp需要配合過濾器使用,或者使用shiro架構######但是一般的靜態資源我們不需要過濾攔截,可以使用如下配置#####web.xml配置### ######     default###     *.css*.js###     /image/*###  #####spring-serv###sp. .xml配置,注意頭部檔案的新增#########        ###### ######################################################################C

以上是java中過濾器、監聽器、攔截器機制的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn