過濾器(filter)與攔截器(Interceptor ) 差別性比較

1.過濾器

Servlet中的過濾器Filter是實現了javax.servlet.Filter接口的服務器端程序,主要的用途是過濾字符編碼、作一些業務邏輯判斷等。其工做原理是,只要你在web.xml文件配置好要攔截的客戶端請求,它都會幫你攔截到請求,此時你就能夠對請求或響應(Request、Response)統一設置編碼,簡化操做;同時還可進行邏輯判斷,如用戶是否已經登錄、有沒有權限訪問該頁面等等工做。它是隨你的web應用啓動而啓動的,只初始化一次,之後就能夠攔截相關請求,只有當你的web應用中止或從新部署的時候才銷燬,如下經過過濾編碼的代碼示例來了解它的使用:html

// 主要目的:過濾字符編碼;其次,作一些應用邏輯判斷等.   
// 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初始化...");   
     }   
 }   

chain.doFilter(request, response);java

這個方法的調用做爲分水嶺。事實上調用Servlet的doService()方法是在chain.doFilter(request, response);這個方法中進行的。web

如下是 MyCharsetFilter.java 在web.xml 中配置: spring

<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.攔截器

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

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  {  
    Object result = null;  
    System.out.println("before invoke method :" + method.getName());  
    result = method.invoke(this.targetObj, args);  
    System.out.println("after invoke method : " + method.getName());  
    return result;  
}  

三、差別性比較

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

  • 攔截器:主要是用在插件上,擴展件上好比 hivernate spring struts2等 有點相似面向切片的技術,在用以前先要在配置文件即xml文件裏聲明一段的那個東西。app

  1. 攔截器是基於java的反射機制的,而過濾器是基於函數回調。
  2. 攔截器不依賴與servlet容器,過濾器依賴與servlet容器。 
  3. 攔截器只能對action請求起做用,而過濾器則能夠對幾乎全部的請求起做用。
  4. 攔截器能夠訪問action上下文、值棧裏的對象,而過濾器不能訪問。 
  5. 在action的生命週期中,攔截器能夠屢次被調用,而過濾器只能在容器初始化時被調用一次

四、調用順序

執行順序圖

image

圖片來源:http://blog.csdn.net/u013378306/article/details/50357375函數

參考連接

相關文章
相關標籤/搜索