過濾器、監聽器、攔截器的區別

1.過濾器
 
Servlet中的過濾器Filter是實現了javax.servlet.Filter接口的服務器端程序,主要的用途是過濾字符編碼、 作一些業務邏輯判斷等。其工做原理是,只要你在web.xml文件配置好要攔截的客戶端請求,它都會幫你攔截到請求,此時你就能夠對請求或響應(Request、Response)統一設置編碼,簡化操做;同時還可進行邏輯判斷,如用戶是否已經登錄、有沒有權限訪問該頁面等等工做。它是隨你的web應用啓動而啓動的,只初始化一次, 之後就能夠攔截相關請求,只有當你的web應用中止或從新部署的時候才銷燬
  
 主要目的:過濾字符編碼;其次,作一些應用邏輯判斷等. Filter跟web應用一塊兒啓動 當web應用從新啓動或銷燬時,Filter也被銷燬
 
 
public class MyCharsetFilter implements Filter  {
private FilterConfig config = null;
 
public void destroy() {
System.out.println("MyCharsetFilter準備銷燬...");
}
 
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
// 強制類型轉換
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
// 獲取web.xm設置的編碼集,設置到Request、Response中
request.setCharacterEncoding(config.getInitParameter("charset"));
response.setContentType(config.getInitParameter("contentType"));
response.setCharacterEncoding(config.getInitParameter("charset"));
// 將請求轉發到目的地
chain.doFilter(request, response);
}
 
public void init(FilterConfig arg0) throws ServletException {
this.config = arg0;
System.out.println("MyCharsetFilter初始化...");
}
}
 
<!-- 如下是 MyCharsetFilter.java 在web.xml 中配置: -->
<filter>
<filter-name>filter</filter-name>
<filter-class>dc.gz.filters.MyCharsetFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>contentType</param-name>
<param-value>text/html;charset=UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<!-- * 表明截獲全部的請求 或指定請求/test.do /xxx.do -->
<url-pattern>/*</url-pattern>
</filter-mapping>

2.監聽器

Servlet容器提供了多種監聽器的接口,使用的時候根據需求選擇特定的接口實現就行。這些接口都是繼承的java.util包中的EventListener接口。       html

 

 能夠看出,監聽器的種類確實不少。java

       經常使用的有:mysql

        (1)request對象的監聽ServletRequestAttributeListener和ServletRequestListenerweb

             ServletRequestListener用於監聽用戶請求的建立和銷燬。spring

             ServletRequestAttributeListener則用於監聽ServletRequest(request)範圍內屬性的變化,實現該接口的監聽器須要實現attributeAdded、attributeRemoved、attributeReplaced三個方法,分別監聽Request的增長,刪除和修改。sql

       (2)Session對象的監聽:HttpSessionAttributeListener和HttpSessionListener  數據庫

             HttpSessionListener監聽HttpSession的操做。當建立一個Session時,激發sessionCreated(SessionEvent se)方法;當銷燬一個 Session時,激發sessionDestroyed (HttpSessionEvent se)方法。
             HttpSessionAttributeListener監聽HttpSession中的屬性的操做。當在Session增長一個屬性時,激發                          attributeAdded(HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發                                                    attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被從新設置時,激發              attributeReplaced(HttpSessionBindingEvent se) 方法。編程

      (3)ServletContext對象的監聽:ServletContextAttributeListener和ServletContextListener。服務器

           ServletContextAttributeListener監聽對ServletContext屬性的操做,好比增長/刪除/修改    session

           ServletContextListener監聽ServletContext,當建立ServletContext時,激發 contextInitialized                    (ServletContextEvent sce)方法;當銷燬ServletContext時,激發 contextDestroyed(ServletContextEvent sce)方法。


監聽器Listener,它是實現了javax.servlet.ServletContextListener 接口的服務器端程序,它也是隨web應用的啓動而啓動,只初始化一次,隨web應用的中止而銷燬。主要做用是: 作一些初始化的內容添加工做、設置一些基本的內容、好比一些參數或者是一些固定的對象等等。下面利用監聽器對數據庫鏈接池DataSource的初始化演示它的使用:

public class MyServletContextListener implements ServletContextListener {
// 應用監聽器的銷燬方法
public void contextDestroyed(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
// 在整個web應用銷燬以前調用,將全部應用空間所設置的內容清空
sc.removeAttribute("dataSource");
System.out.println("銷燬工做完成...");
}
 
// 應用監聽器的初始化方法
public void contextInitialized(ServletContextEvent event) {
// 經過這個事件能夠獲取整個應用的空間
// 在整個web應用下面啓動的時候作一些初始化的內容添加工做
ServletContext sc = event.getServletContext();
// 設置一些基本的內容;好比一些參數或者是一些固定的對象
// 建立DataSource對象,鏈接池技術 dbcp
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql://localhost:3306/hibernate");
bds.setUsername("root");
bds.setPassword("root");
bds.setMaxActive(10);// 最大鏈接數
bds.setMaxIdle(5);// 最大管理數
// bds.setMaxWait(maxWait); 最大等待時間
// 把 DataSource 放入ServletContext空間中,
// 供整個web應用的使用(獲取數據庫鏈接)
sc.setAttribute("dataSource", bds);
System.out.println("應用監聽器初始化工做完成...");
System.out.println("已經建立DataSource...");
}
}

<!-- web.xml中配置以下,很簡單: -->
 
<!-- 配置應用監聽器 -->
<listener>
<listener-class>dc.gz.listeners.MyServletContextListener</listener-class>
</listener>
 
這樣配置好了以後,之後在web應用中就能夠經過ServletContext取得BasicDataSource對象,從而獲取與數據庫的鏈接,提升性能,方便使用。

3.攔截器
 
攔截器是在面向切面編程中應用的,就是在你的service或者一個方法前調用一個方法,或者在方法後調用一個方法。是基於JAVA的反射機制。 

 

public class MyInterceptor implements Interceptor {
public void destroy() {
// TODO Auto-generated method stub
}
 
public void init() {
// TODO Auto-generated method stub
}
 
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Action執行前插入 代碼");
// 執行目標方法 (調用下一個攔截器, 或執行Action)
final String res = invocation.invoke();
System.out.println("Action執行後插入 代碼");
return res;
}
}
        <!--Struts2攔截器須要在struts.xml中聲明-->
    <struts>
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="MyInterceptor" class="interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="MyInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="loginAction" class="loginAction">
<result name="fail">/index.jsp </result>
<result name="success">/success.jsp</result>
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
</package>
</struts> 

總結:

1.過濾器:所謂過濾器顧名思義是用來過濾的,在java web中,你傳入的request,response提早過濾掉一些信息,或者提早設置一些參數,而後再傳入servlet或者struts的action進行業務邏輯,好比過濾掉非法url(不是login.do的地址請求,若是用戶沒有登錄都過濾掉),或者在傳入servlet或者struts的action前統一設置字符集,或者去除掉一些非法字符(聊天室常常用到的,一些罵人的話)。filter 流程是線性的, url傳來以後,檢查以後,可保持原來的流程繼續向下執行,被下一個filter, servlet接收等.

2.監聽器:這個東西在c/s模式裏面常常用到,他會對特定的事件產生產生一個處理。監聽在不少模式下用到。好比說觀察者模式,就是一個監聽來的。又好比struts能夠用監聽來啓動。Servlet監聽器用於監聽一些重要事件的發生,監聽器對象能夠在事情發生前、發生後能夠作一些必要的處理。

3.java的攔截器 主要是用在插件上,擴展件上好比 hivernate spring struts2等 有點相似面向切片的技術

4.Filter基於回調函數,咱們須要實現的filter接口中doFilter方法就是回調函數,而interceptor則基於java自己的反射機制,這是二者最本質的區別。

Filter是依賴於servlet容器的,即只能在servlet容器中執行,很顯然沒有servlet容器就沒法來回調doFilter方法。而interceptor與servlet容器無關。
Filter的過濾範圍比Interceptor大,Filter除了過濾請求外經過通配符能夠保護頁面,圖片,文件等等,而Interceptor只能過濾請求。

Filter的過濾例外通常是在加載的時候在init方法聲明,而Interceptor能夠經過在xml聲明是guest請求仍是user請求來辨別是否過濾 

相關文章
相關標籤/搜索