Java攔截器與過濾器

@、攔截器是基於java的反射機制的,而過濾器是基於函數回調 
@、過濾器依賴與servlet容器,而攔截器不依賴與servlet容器 
@、攔截器只能對action請求起作用,而過濾器則可以對幾乎所有的請求起作用 
@、攔截器可以訪問action上下文、值棧裏的對象,而過濾器不能 
@、在action的生命週期中,攔截器可以多次被調用,而過濾器只能在容器初始化時被調用一次 

     攔截器 :是在面向切面編程的就是在你的service或者一個方法,前調用一個方法,或者在方法後調用一個方法比如動態代理就是攔截器的簡單實現,在你調用方法前打印出字符串(或者做其它業務邏輯的操作),也可以在你調用方法後打印出字符串,甚至在你拋出異常的時候做業務邏輯的操作。 

下面通過實例來看一下過濾器和攔截器的區別: 

使用攔截器進行/admin 目錄下jsp頁面的過濾 

<package name="newsDemo" extends="struts-default" 
        namespace="/admin"> 
        <interceptors> 
            <interceptor name="auth" class="com.test.news.util.AccessInterceptor" /> 
            <interceptor-stack name="authStack"> 
                <interceptor-ref name="auth" /> 
            </interceptor-stack> 
        </interceptors> 
        <!-- action --> 
        <action name="newsAdminView!*" class="newsAction" 
            method="{1}"> 
            <interceptor-ref name="defaultStack"/> 
            <interceptor-ref name="authStack"> 
            </interceptor-ref> 

下面是我實現的Interceptor class: 

package com.test.news.util; 

import java.util.Map; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 
import com.test.news.action.AdminLoginAction; 

/** 
* @author chaoyin 
*/ 

public class AccessInterceptor extends AbstractInterceptor { 

    private static final long serialVersionUID = -4291195782860785705L; 

    @Override 
    public String intercept(ActionInvocation actionInvocation) throws Exception { 
         ActionContext actionContext = actionInvocation.getInvocationContext(); 
         Map session = actionContext.getSession(); 
        
        //except login action 
         Object action = actionInvocation.getAction(); 
        if (action instanceof AdminLoginAction) { 
            return actionInvocation.invoke(); 
         } 
        //check session 
        if(session.get("user")==null ){ 
            return "logout"; 
         } 
        return actionInvocation.invoke();//go on 
     } 



       過濾器:是在java web中,你傳入的request,response提前過濾掉一些信息,或者提前設置一些參數,然後再傳入servlet或者struts的 action進行業務邏輯,比如過濾掉非法url(不是login.do的地址請求,如果用戶沒有登陸都過濾掉),或者在傳入servlet或者 struts的action前統一設置字符集,或者去除掉一些非法字符. 

使用過濾器進行/admin 目錄下jsp頁面的過濾,首先在web.xml進行過濾器配置: 

    <filter> 
        <filter-name>access filter</filter-name> 
        <filter-class> 
             com.test.news.util.AccessFilter 
        </filter-class> 
    </filter> 
    <filter-mapping> 
        <filter-name>access filter</filter-name> 
        <url-pattern>/admin/*</url-pattern> 
    </filter-mapping> 


下面是過濾的實現類: 

package com.test.news.util; 

import java.io.IOException; 

import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 


public class AccessFilter implements Filter { 

/** 
* @author chaoyin 
*/ 
    
    public void destroy() { 

     } 

    public void doFilter(ServletRequest arg0, ServletResponse arg1, 
             FilterChain filterChain) throws IOException, ServletException { 
         HttpServletRequest request = (HttpServletRequest)arg0; 
         HttpServletResponse response = (HttpServletResponse)arg1; 
         HttpSession session = request.getSession(); 
        if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){ 
             response.sendRedirect("login.jsp"); 
            return ; 
         } 
         filterChain.doFilter(arg0, arg1); 

     } 

    public void init(FilterConfig arg0) throws ServletException { 

     } 

}
發佈了29 篇原創文章 · 獲贊 15 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章