A. Filter:
Filter, also known as filters, is a more exciting technology in Servlet technology, Web developers through the filter technology, Web server management of all Web resources: such as JSP, Servlet, static picture files or static HTML files, etc. to intercept, So that some special functions can be realized. For example, the implementation of URL-level access control, filtering sensitive words, compressed response information and other advanced features.
1.Filter Introduction:
The Servlet API provides a filter interface that, when developing a Web application, if the Java class is written to implement this interface, the Java class is called filter filter. Filter technology enables developers to intercept requests and responses to access before they access a target resource. Simply put, it is possible to implement a Web container for the pre-access interception of a resource before processing it, or to intercept a resource before it returns a response to the Web container.
2. Configuration and application: Filter is tied to the servlet, so the general filter configuration is directly in Web. xml
<filter> <filter-name>Demo1Filter</filter-name> <filter-class> com.itheima.filter.demo1filter</filter-class> <init-param> <param-name>param1</ Param-name> <param-value>value is here. </param-value> </init-param> </filter> < filter-mapping> <filter-name>Demo1Filter</filter-name> <url-pattern>/*</ Url-pattern> <dispatcher>REQUEST</dispatcher> <!--Not Configured dispatcher is the default REQUEST method-- <dispatcher>FORWARD</dispatcher> <dispatcher>ERROR</dispatcher>
Two. Interceptor: Mainly about spring MVC interceptor
Reference http://haohaoxuexi.iteye.com/blog/1750680
Note that I have tried to get some parameters of the intercepted method, but it is not available through methodparaters, and then only through Request.getparameter (..) To get. Also, note the execution time points of Posthandle and aftercompletion in the article
Interceptor configuration in Spring MVC:
<mvc:interceptors><mvc:interceptor><mvc:mapping path= "/**"/><bean class= " Interceptor.loginterceptor "/></mvc:interceptor></mvc:interceptors>
Three. The difference between filter and interceptor:
1.filter based on the Dofilter callback function in the filter interface, interceptor is based on the reflection mechanism of Java itself
2.filter is dependent on the servlet container, and the Dofilter method cannot be recalled without a servlet container, and interceptor is not relevant to the servlet
3. Filter filtering range is larger than interceptor, filter in addition to the filter request through wildcards can protect the page, pictures, files, etc., and interceptor can only filter the request, only the action, before the action to start, Ends when the action is completed (such as being intercepted, not executing action);
4. Filter filtering range is larger than interceptor, filter in addition to the filter request through wildcards can protect the page, pictures, files, etc., and interceptor can only filter the request, only the action, before the action to start, Ends when the action is completed (such as being intercepted, not executing action);
5.interceptor can access the action context, the object in the value stack, and filter cannot;
6. In the life cycle of an action, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized.
Four. The difference between interceptor and spring AOP
1.spring Interceptor is also an AOP idea, our spring AOP is mainly about AOP applications, the use of interceptor is much smaller than AOP, as the name implies, it is to intercept some action requests, but it is easier to use than AOP , and the session information can be obtained, in line with the layered design, so individuals prefer to use interceptor.
2.Interceptor can prevent the code from executing, when Prehandle returns false, then this request is over, the real intercept, but AOP can not, it is simply cut into the add operation
3.AOP uses dynamic proxies, while interceptor is based on a reflection mechanism. So, the proceedingjoinpoint of AOP can provide more parameters
The 4.AOP is able to control the execution time of the intercepted method, that is, when it executes, but it must be executed, typically in @around advice.
The 5.AOP Pointcut is also able to make it more flexible to do some aspect work, while the non-request method can only use AOP.
Filter, INTERCEPTOR,AOP