拦截器和过滤器的区别

本文详细阐述了拦截器和过滤器的区别,包括工作原理、依赖环境及应用场景等,并通过具体示例说明两者如何实现对/admin目录下jsp页面的访问控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

拦截器和过滤器的区别:

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

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

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

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

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

   

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

   

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

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

[html]    

<packagename="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-->   

    <actionname="newsAdminView!*" class="newsAction"  method="{1}">   

    <interceptor-ref  name="defaultStack"/>   

    <interceptor-ref  name="authStack"> </interceptor-ref>

</package>

   

下面是我实现的Interceptor class:   

   [java]   

    package com.test.news.util;   

    import java.util.Map;   

    importcom.opensymphony.xwork2.ActionContext;   

    importcom.opensymphony.xwork2.ActionInvocation;   

   import com.opensymphony.xwork2.interceptor.AbstractInterceptor;   

    importcom.test.news.action.AdminLoginAction;   

    /**

    * @author

    */   

    public class AccessInterceptor extendsAbstractInterceptor {   

             privatestatic final long serialVersionUID = -4291195782860785705L;   

             @Override

   

public String intercept(ActionInvocation actionInvocation) throws Exception {   

                         ActionContext actionContext =actionInvocation.getInvocationContext();   

                        Mapsession = actionContext.getSession();   

                       //exceptlogin action   

                       Objectaction = actionInvocation.getAction();   

                       if(action instanceof AdminLoginAction) {   

                                returnactionInvocation.invoke();   

                       }

   

                       //checksession   

                       if(session.get("user")==null ){   

                                return"logout";   

                       }   

                       returnactionInvocation.invoke();//go on   

             }   

    }

   

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

   

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

   [html]    

    <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>

   

下面是过滤的实现类:

   

    [java]   

    packagecom.test.news.util;   

    importjava.io.IOException;   

    importjavax.servlet.Filter;

    importjavax.servlet.FilterChain;   

    importjavax.servlet.FilterConfig;   

    importjavax.servlet.ServletException;   

    import javax.servlet.ServletRequest;   

    importjavax.servlet.ServletResponse;   

    importjavax.servlet.http.HttpServletRequest;   

    importjavax.servlet.http.HttpServletResponse;   

    importjavax.servlet.http.HttpSession;   

    public classAccessFilter implements Filter {   

   

                  public void init(FilterConfig arg0) throws ServletException { }

             public void destroy() { }

   

             public void doFilter(ServletRequest request, ServletResponse response,   

               FilterChain filterChain) throws IOException,ServletException {

   

                       HttpServletRequest request = (HttpServletRequest)request;   

                       HttpServletResponse response = (HttpServletResponse)response;   

                       HttpSession session = request.getSession();   

                       if(session.getAttribute("user")== null

&& request.getRequestURI().indexOf("login.jsp")==-1 ){   

                                response.sendRedirect("login.jsp");   

                                return ;   

                        }   

                       filterChain.doFilter(request, response);

             }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值