`
zhongzhengmin
  • 浏览: 29301 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

过滤器和拦截器的区别

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

相关推荐

Global site tag (gtag.js) - Google Analytics