Filter与Interceptor的区别

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


我的见解:

1. 以下是struts中的Interceptor说明:

An interceptor is a stateless class that follows the interceptor pattern, as found injavax.servlet.Filter and in AOP languages.

Interceptors are objects that dynamically intercept Action invocations. They provide the developer with the opportunity to define code that can be executed before and/or after the execution of an action. They also have the ability to prevent an action from executing. Interceptors provide developers a way to encapulate common functionality in a re-usable form that can be applied to one or more Actions.

Interceptors must be stateless and not assume that a new instance will be created for each request or Action. Interceptors may choose to either short-circuit theActionInvocation execution and return a return code (such ascom.opensymphony.xwork2.Action.SUCCESS), or it may choose to do some processing before and/or after delegating the rest of the procesing usingActionInvocation.invoke().  

从此,可以看出,Interceptor的设计目的是为了可重用,设计的思想来自于AOP。

 

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


我的见解:

看看Servlet规范中的Filter的定义:

A filter is a reusable piece of code that can transform the content of HTTP requests, responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource. Filters can act on dynamic or static content. 
Among the types of functionality available to the developer needing to use filters are the following:
• The accessing of a resource before a request to it is invoked.
• The processing of the request for a resource before it is invoked.

• the modification of request headers and data by wrapping the request in customized versions of the request object.

• The modification of response headers and response data by providing customized versions of the response object.
• The interception of an invocation of a resource after its call.
• Actions on a servlet, on groups of servlets, or static content by zero, one, or more filters in a specifiable order.

从此可见, filter也是可重用的,filter的设计目的是修改request或者response,它也可以拦截invocation.

个人以为,interceptor是一种广泛的设计模式,从设计模式的角度来讲,它甚至可以包含filter,filter主要在servlet环境中使用。

单从struts来说,interceptor做的事情filter都能做,比如说拦截action,形成filter链。这估计就是为什么struts中使用了interceptor来完成过滤拦截的功能,因为在servlet环境中,filter主要就用在修改request和response。使用interceptor模式,更好的服从servlet规范中filter的定义,而不是将拦截这样的功能也以filter来实现。

从定义上看,二者还是有很多重合和功能类似的地方。设计的目的都是为了可重用。

具体区别:


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

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

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

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

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


目前的理解就是这样,大家有什么好的理解,就放在下面的评论里,黑体是我自己写的,其余的是转载的别人的资料。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值