java過濾器、監聽器、攔截器機制

 

 

1、過濾器css

Filter也稱之爲過濾器,它是Servlet技術中最實用的技術,Web開發人員經過Filter技術,對web服務器管理的全部web資源:例如Jsp, Servlet, 靜態圖片文件或靜態 html 文件等進行攔截,從而實現一些特殊的功能。例如實現URL級別的權限訪問控制、過濾敏感詞彙、壓縮響應信息等一些高級功能。html

它主要用於對用戶請求進行預處理,也能夠對HttpServletResponse進行後處理。使用Filter的完整流程:Filter對用戶請求進行預處理,接着將請求交給Servlet進行處理並生成響應,最後Filter再對服務器響應進行後處理。java

通常與spring架構一塊兒用在如下幾個地方:web

1.1 處理字符寫入數據庫編碼問題,在web.xml中配置一下代碼spring

 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完成代理功能mongodb

 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
過濾器生命週期的四個階段:
一、實例化:Web容器在部署Web應用程序時對全部過濾器進行實例化。Web容器回調它的無參構造方法。
二、初始化:實例化完成以後,立刻進行初始化工做。Web容器回調init()方法。
三、過濾:請求路徑匹配過濾器的URL映射時。Web容器回調doFilter()方法——主要的工做方法。
四、銷燬: Web容器在卸載Web應用程序前,Web容器回調destroy()方法。

 

2、監聽器數據庫

監聽器Listener就是在application,session,request三個對象建立、銷燬或者往其中添加修改刪除屬性時自動執行代碼的功能組件。緩存

Listener是Servlet的監聽器,能夠監聽客戶端的請求和服務端的操做等。tomcat

主要有如下三類:服務器

一、ServletContext監聽

ServletContextListener:用於對Servlet整個上下文進行監聽(建立、銷燬)。
ServletContextAttributeListener:對Servlet上下文屬性的監聽(增刪改屬性)。

二、Session監聽

Session屬於http協議下的內容,接口位於javax.servlet.http.*包下。

HttpSessionListener接口:對Session的總體狀態的監聽。
HttpSessionAttributeListener接口:對session的屬性監聽。
session的銷燬有兩種狀況:

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

<!--session120分鐘後超時銷燬--> </session-config>   

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

三、Request監聽

ServletRequestListener:用於對Request請求進行監聽(建立、銷燬)。
ServletRequestAttributeListener:對Request屬性的監聽(增刪改屬性)。

四、在web.xml中配置

Listener配置信息必須在Filter和Servlet配置以前,Listener的初始化(ServletContentListener初始化)比Servlet和Filter都優先,

而銷燬比Servlet和Filter都慢。

<listener> <listener-class>com.listener.class</listener-class> </listener>

<context-param> <param-name>contextConfigLocation</param-name>

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

<!-- 採用的是通配符方式,查找WEB-INF/spring目錄下xml文件。若有多個xml文件,以「,」分隔。 -->

</context-param>

<listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

四、1 Spring使用IntrospectorCleanupListener清理緩存

這個監聽器的做用是在web應用關閉時刷新JDK的JavaBeans的Introspector緩存,以確保Web應用程序的類加載器以及其加載的類正確的釋放資源。

若是JavaBeans的Introspector已被用來分析應用程序類,系統級的Introspector緩存將持有這些類的一個硬引用。所以,這些類和Web應用程序的類加載器在Web應用程序關閉時將不會被垃圾收集器回收!而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應用的生命週期的適當時機生效。

<!-- memory clean -->

<listener>

   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

3、spring攔截器

spring 管理的攔截器須要繼承Intercepter implements HandlerInterceptor

要進入攔截器優先須要配置spring的入口

<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

 以上這種寫法不會過濾掉.jsp結尾的url,若須要處理.jsp須要配合過濾器使用,或者使用shiro架構

可是通常的靜態資源咱們不須要過濾攔截,可使用以下配置

web.xml配置

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
  <url-pattern>*.js</url-pattern>
  <url-pattern>/image/*</url-pattern>
</servlet-mapping>

spring-servlet.xml配置,注意頭部文件的添加

<mvc:annotation-driven/> <mvc:default-servlet-handler/>

相關文章
相關標籤/搜索