Struts2中过滤器和拦截器的区别

来源:互联网 发布:mac sass安装教程 编辑:程序博客网 时间:2024/04/28 06:34

拦截器和过滤器的区别:

1、拦截器是基于java的反射机制的,而过滤器是基于函数回调

2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器

3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用

4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能

5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次

 拦截器 :是在面向切面编程的就是在你的service或者一个方法前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业务逻辑的操作),也可以在你调用方法后打印出字符串,甚至在你抛出异常的时候做业务逻辑的操作。

下面通过实例来看一下过滤器和拦截器的区别:

使用拦截器进行/admin 目录下jsp页面的过滤


  1. <package name="newsDemo" extends="struts-default"   
  2.         namespace="/admin">   
  3.         <interceptors>   
  4.             <interceptor name="auth" class="com.test.news.util.AccessInterceptor" />   
  5.             <interceptor-stack name="authStack">   
  6.                 <interceptor-ref name="auth" />   
  7.             </interceptor-stack>   
  8.         </interceptors>   
  9.         <!-- action -->   
  10.         <action name="newsAdminView!*" class="newsAction"   
  11.             method="{1}">   
  12.             <interceptor-ref name="defaultStack"/>   
  13.             <interceptor-ref name="authStack">   
  14.             </interceptor-ref>   


下面是我实现的Interceptor class: 

  1. package com.test.news.util;   
  2. import java.util.Map;   
  3. import com.opensymphony.xwork2.ActionContext;   
  4. import com.opensymphony.xwork2.ActionInvocation;   
  5. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;   
  6. import com.test.news.action.AdminLoginAction;   
  7. /**  
  8. * @author chaoyin  
  9. */   
  10. public class AccessInterceptor extends AbstractInterceptor {   
  11.     private static final long serialVersionUID = -4291195782860785705L;   
  12.     @Override   
  13.     public String intercept(ActionInvocation actionInvocation) throws Exception {   
  14.          ActionContext actionContext = actionInvocation.getInvocationContext();   
  15.          Map session = actionContext.getSession();   
  16.           
  17.         //except login action   
  18.          Object action = actionInvocation.getAction();   
  19.         if (action instanceof AdminLoginAction) {   
  20.             return actionInvocation.invoke();   
  21.          }   
  22.         //check session   
  23.         if(session.get("user")==null ){   
  24.             return "logout";   
  25.          }   
  26.         return actionInvocation.invoke();//go on   
  27.      }   
  28. }   

 过滤器:是在javaweb中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符.

使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置: 


  1. <filter>   
  2.         <filter-name>access filter</filter-name>   
  3.         <filter-class>   
  4.              com.test.news.util.AccessFilter   
  5.         </filter-class>   
  6.     </filter>   
  7.     <filter-mapping>   
  8.         <filter-name>access filter</filter-name>   
  9.         <url-pattern>/admin/*</url-pattern>   
  10.     </filter-mapping>   


下面是过滤的实现类: 


  1. package com.test.news.util;   
  2. import java.io.IOException;   
  3. import javax.servlet.Filter;   
  4. import javax.servlet.FilterChain;   
  5. import javax.servlet.FilterConfig;   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.ServletRequest;   
  8. import javax.servlet.ServletResponse;   
  9. import javax.servlet.http.HttpServletRequest;   
  10. import javax.servlet.http.HttpServletResponse;   
  11. import javax.servlet.http.HttpSession;   
  12. public class AccessFilter implements Filter {   
  13. /**  
  14. * @author chaoyin  
  15. */   
  16.       
  17.     public void destroy() {   
  18.      }   
  19.     public void doFilter(ServletRequest arg0, ServletResponse arg1,   
  20.              FilterChain filterChain) throws IOException, ServletException {   
  21.          HttpServletRequest request = (HttpServletRequest)arg0;   
  22.          HttpServletResponse response = (HttpServletResponse)arg1;   
  23.          HttpSession session = request.getSession();   
  24.         if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){   
  25.              response.sendRedirect("login.jsp");   
  26.             return ;   
  27.          }   
  28.          filterChain.doFilter(arg0, arg1);   
  29.      }   
  30.     public void init(FilterConfig arg0) throws ServletException {   
  31.      }   
  32. }     

0 0