java 拦截器和过滤器区别(转载)

1、拦截器是基于java的反射机制的,而过滤器是基于函数回调 
2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 
3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用 
4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能 
5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次 

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

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

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

Xml代码

 收藏代码

  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>   
  15. </package>  

 

Java代码

 收藏代码

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

 

Java代码

 收藏代码

  1.    过滤器:是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符.   
  2.   
  3. 使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置:   
  4.   
  5.     <filter>   
  6.         <filter-name>access filter</filter-name>   
  7.         <filter-class>   
  8.              com.test.news.util.AccessFilter   
  9.         </filter-class>   
  10.     </filter>   
  11.     <filter-mapping>   
  12.         <filter-name>access filter</filter-name>   
  13.         <url-pattern>/admin/*</url-pattern>  
  14.     </filter-mapping>  
  15.  
  16.  
  17. 下面是过滤的实现类:  
  18.  
  19. package com.test.news.util;  
  20.  
  21. import java.io.IOException;  
  22.  
  23. import javax.servlet.Filter;  
  24. import javax.servlet.FilterChain;  
  25. import javax.servlet.FilterConfig;  
  26. import javax.servlet.ServletException;  
  27. import javax.servlet.ServletRequest;  
  28. import javax.servlet.ServletResponse;  
  29. import javax.servlet.http.HttpServletRequest;  
  30. import javax.servlet.http.HttpServletResponse;  
  31. import javax.servlet.http.HttpSession;  
  32.  
  33.  
  34. public class AccessFilter implements Filter {  
  35.  
  36. /**  
  37. @author chaoyin  
  38. */   
  39.       
  40.     public void destroy() {   
  41.   
  42.      }   
  43.   
  44.     public void doFilter(ServletRequest arg0, ServletResponse arg1,   
  45.              FilterChain filterChain) throws IOException, ServletException {   
  46.          HttpServletRequest request = (HttpServletRequest)arg0;   
  47.          HttpServletResponse response = (HttpServletResponse)arg1;   
  48.          HttpSession session = request.getSession();   
  49.         if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){   
  50.              response.sendRedirect("login.jsp");   
  51.             return ;   
  52.          }   
  53.          filterChain.doFilter(arg0, arg1);   
  54.   
  55.      }   
  56.   
  57.     public void init(FilterConfig arg0) throws ServletException {   
  58.   
  59.      }   
  60.   
  61. }  

转载于:https://my.oschina.net/u/3049580/blog/914666

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值