Difference between filter and interceptor

(1) Because the url-pattern mapped in the filter at the beginning fills in the path as *.action. All actions are filtered through it. <url-pattern>*.action</url-pattern>

(2) Because the filter is configured in web.xml, all programs in the execution process go through the struts2 filter (path is: /*), first filter your own related programs, such as xx.action, process first, if it is .html or .jsp will not be processed and will be released directly.

Solution: Change the filter mapping path to: /*, that is, all filter processing.

<url-pattern>/*</url-pattern>

Different from filters and interceptors, interceptors are the core of struts.

 

understand

(1) Filter: When you have a bunch of things, you only want to select certain things that meet your requirements. The tool for defining these requirements is the filter. (Understanding: it is to take a B from a bunch of letters)

(2) Interceptor: When a process is in progress, you want to intervene in its progress, or even terminate its progress. This is what the interceptor does. (Understanding: it is a pile of letters, intervene in him, pass the verification less, and do something else by the way).

Interceptor: Aspect-oriented programming is to call a method before your service or a method, or call a method after the method. For example, a dynamic proxy is a simple implementation of an interceptor, which prints out a string before you call the method ( Or do other business logic operations), you can also print out a string after you call the method, or even do business logic operations when you throw an exception.

Filter: In javaweb, the request and response you pass in filter out some information in advance, or set some parameters in advance, and then pass in the action of servlet or struts for business logic, such as filtering out illegal urls (not login.do If the user does not log in, it will be filtered out), or set the character set uniformly before the action of the incoming servlet or struts, or remove some illegal characters.

 

Interceptor and filter usage scenarios:

spring Web MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。

 

1、日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。

2、权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面;

3、性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);

4、通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个处理器都需要的即可使用拦截器实现。

5、OpenSessionInView:如hibernate,在进入处理器打开Session,在完成后关闭Session。

 

 

具体区别

 

拦截器是AOP( Aspect-Oriented Programming)的一种实现,底层通过动态代理模式完成。

区别:

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

(2)拦截器不依赖于servlet容器,而过滤器依赖于servlet容器。

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

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

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

 

 

 两者的本质区别:从灵活性上说拦截器功能更强大些,Filter能做的事情,他都能做,而且可以在请求前,请求后执行,比较灵活。Filter主要是针对URL地址做一个编码的事情、过滤掉没用的参数、安全校验(比较泛的,比如登录不登录之类),太细的话,还是建议用interceptor。不过还是根据不同情况选择合适的。

 

 

 

 

filter

Interceptor

多个的执行顺序

根据filter mapping配置的先后顺序

按照配置的顺序,但是可以通过order控制顺序

规范

在Servlet规范中定义的,是Servlet容器支持的

spring容器内的,是Spring框架支持的。

使用范围

只能用于Web程序中

既可以用于Web程序,也可以用于Application、Swing程序中。

深度

Filter在只在Servlet前后起作用

拦截器能够深入到方法前后、异常抛出前后等

 

 

补充说明:
    Spring的拦截器与Servlet的Filter有相似之处,比如二者都是AOP编程思想的体现,都能实现权限检查、日志记录等。不同的是:

     使用范围不同:Filter是Servlet规范规定的,只能用于Web程序中。而拦截器既可以用于Web程序,也可以用于Application、Swing程序中。
     规范不同:Filter是在Servlet规范中定义的,是Servlet容器支持的。而拦截器是在Spring容器内的,是Spring框架支持的。

使用的资源不同:同其他的代码块一样,拦截器也是一个Spring的组件,归Spring管理,配置在Spring文件中,因此能使用Spring里的任何资源、对象,例如Service对象、数据源、事务管理等,通过IoC注入到拦截器即可;而Filter则不能。

深度不同:Filter在只在Servlet前后起作用。而拦截器能够深入到方法前后、异常抛出前后等,因此拦截器的使用具有更大的弹性。所以在Spring构架的程序中,要优先使用拦截器。

  实际上Filter和Servlet极其相似,区别只是Filter不能直接对用户生成响应。实际上Filter里doFilter()方法里的代码就是从多个Servlet的service()方法里抽取的通用代码,通过使用Filter可以实现更好的复用。
  filter是一个可以复用的代码片段,可以用来转换HTTP请求、响应和头信息。Filter不像Servlet,它不能产生一个请求或者响 应,它只是修改对某一资源的请求,或者修改从某一的响应。
  JSR中说明的是,按照多个匹配的Filter,是按照其在web.xml中配置的顺序 来执行的。 

所以这也就是,把自己的Filter或者其他的Filter(比如UrlRewrite的Filter)放在Struts的 DispatcherFilter的前面的原因。因为,它们需要在请求被Struts2框架处理之前,做一些前置的工作。 

        当Filter被调用,并且进入了Struts2的DispatcherFilter中 后,Struts2会按照在Action中配置的Interceptor Stack中的Interceptor的顺序,来调用Interceptor


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326011204&siteId=291194637