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


 

 

一、過濾器

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
過濾器生命周期的四個階段:
1、實例化:Web容器在部署Web應用程序時對所有過濾器進行實例化。Web容器回調它的無參構造方法。
2、初始化:實例化完成之后,馬上進行初始化工作。Web容器回調init()方法。
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>120</session-timeout>

<!--session120分鍾后超時銷毀--> </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.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>

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

三、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/>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM