`

过滤器和拦截器的区别

阅读更多
Java代码 复制代码 收藏代码
  1. 过滤器和拦截器的区别   
  2. 1、拦截器是基于java的反射机制的,而过滤器是基于函数回调    
  3. 2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器    
  4. 3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用    
  5. 4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能    
  6. 5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次    
  7.   
  8.      拦截器 :是在面向切面编程的就是在你的service或者一个方法,前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业务逻辑的操作),也可以在你调用方法后打印出字符串,甚至在你抛出异常的时候做业务逻辑的操作。    
  9.   
  10. 下面通过实例来看一下过滤器和拦截器的区别:    
  11.   
  12. 使用拦截器进行/admin 目录下jsp页面的过滤    
  13.   
  14. <package name="newsDemo" extends="struts-default"    
  15.         namespace="/admin">    
  16.         <interceptors>    
  17.             <interceptor name="auth" class="com.test.news.util.AccessInterceptor" />    
  18.             <interceptor-stack name="authStack">    
  19.                 <interceptor-ref name="auth" />    
  20.             </interceptor-stack>    
  21.         </interceptors>    
  22.         <!-- action -->    
  23.         <action name="newsAdminView!*" class="newsAction"    
  24.             method="{1}">    
  25.             <interceptor-ref name="defaultStack"/>    
  26.             <interceptor-ref name="authStack">    
  27.             </interceptor-ref>    
  28.   
  29. 下面是我实现的Interceptor class:    
  30.   
  31. package com.test.news.util;    
  32.   
  33. import java.util.Map;    
  34.   
  35. import com.opensymphony.xwork2.ActionContext;    
  36. import com.opensymphony.xwork2.ActionInvocation;    
  37. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;    
  38. import com.test.news.action.AdminLoginAction;    
  39.   
  40. /**   
  41. * @author chaoyin   
  42. */    
  43.   
  44. public class AccessInterceptor extends AbstractInterceptor {    
  45.   
  46.     private static final long serialVersionUID = -4291195782860785705L;    
  47.   
  48.     @Override    
  49.     public String intercept(ActionInvocation actionInvocation) throws Exception {    
  50.          ActionContext actionContext = actionInvocation.getInvocationContext();    
  51.          Map session = actionContext.getSession();    
  52.            
  53.         //except login action    
  54.          Object action = actionInvocation.getAction();    
  55.         if (action instanceof AdminLoginAction) {    
  56.             return actionInvocation.invoke();    
  57.          }    
  58.         //check session    
  59.         if(session.get("user")==null ){    
  60.             return "logout";    
  61.          }    
  62.         return actionInvocation.invoke();//go on    
  63.      }    
  64.   
  65. }    
  66.   
  67.        过滤器:是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符.    
  68.   
  69. 使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置:    
  70.   
  71.     <filter>    
  72.         <filter-name>access filter</filter-name>    
  73.         <filter-class>    
  74.              com.test.news.util.AccessFilter    
  75.         </filter-class>    
  76.     </filter>    
  77.     <filter-mapping>    
  78.         <filter-name>access filter</filter-name>    
  79.         <url-pattern>/admin/*</url-pattern>   
  80.     </filter-mapping>   
  81.  
  82.  
  83. 下面是过滤的实现类:   
  84.  
  85. package com.test.news.util;   
  86.  
  87. import java.io.IOException;   
  88.  
  89. import javax.servlet.Filter;   
  90. import javax.servlet.FilterChain;   
  91. import javax.servlet.FilterConfig;   
  92. import javax.servlet.ServletException;   
  93. import javax.servlet.ServletRequest;   
  94. import javax.servlet.ServletResponse;   
  95. import javax.servlet.http.HttpServletRequest;   
  96. import javax.servlet.http.HttpServletResponse;   
  97. import javax.servlet.http.HttpSession;   
  98.  
  99.  
  100. public class AccessFilter implements Filter {   
  101.  
  102. /**   
  103. * @author chaoyin   
  104. */    
  105.        
  106.     public void destroy() {    
  107.   
  108.      }    
  109.   
  110.     public void doFilter(ServletRequest arg0, ServletResponse arg1,    
  111.              FilterChain filterChain) throws IOException, ServletException {    
  112.          HttpServletRequest request = (HttpServletRequest)arg0;    
  113.          HttpServletResponse response = (HttpServletResponse)arg1;    
  114.          HttpSession session = request.getSession();    
  115.         if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){    
  116.              response.sendRedirect("login.jsp");    
  117.             return ;    
  118.          }    
  119.          filterChain.doFilter(arg0, arg1);    
  120.   
  121.      }    
  122.   
  123.     public void init(FilterConfig arg0) throws ServletException {    
  124.   
  125.      }    
  126.   
  127. }   
分享到:
评论
1 楼 dafeiwang 2011-11-23  

相关推荐

Global site tag (gtag.js) - Google Analytics