攔截器和過濾器的區別: 一、攔截器是基於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 { } }