Difference between filter and interceptor
preface
Recently, during the interview, I was asked this question and felt that the answer was not very good. I'll sort it out and record it here for my own study. I also hope it can help you.
What is a filter
In Java's javax There is an interface filter under the servlet. Any class that implements the filter interface can be called filter. The main purpose of filter is to set character sets, control permissions, control steering, etc. In the process of using filter, if it is a traditional web project, it has web XML file. We need to configure it in XML. Like this.
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
This method directly changes the character set of our request and response to UTF-8.
If it is used in a project such as spingboot, we need to use the filterregister bean to inject the filter after defining our own filter class.
The filter is started with the start of the project, initialized only once, and destroyed with the stop of the web project.
Filter is mainly used for pre-processing of user requests and post-processing of response returned by programs. You can process the request or add headers and some other data before the request reaches the servlet. Or modify the response header and data before the reponse arrives.
There are three methods in the filter. Void init (filterconfig): used to complete the initialization of filter. Void destruction(): used to recycle resources before the filter is destroyed. Void dofilter (ServletRequest request, servletresponse, filterchain chain): this method is the core filtering method of filter. Request processing is done through request, and then chain. is invoked. doFilter。 After calling, you can handle the response through response.
Interceptor
Interceptor is an implementation scheme of AOP and the embodiment of the idea of AOP. Before we call the method, we call a method of the interceptor or a method of calling the interceptor after the calling method.
Interceptor in spring MVC intercepts requests through handlerinterceptor. Defining an interceptor in spring MVC is very simple. There are two main ways. The first way is that the interceptor class to be defined implements the handlerinterceptor interface of spring, or this class inherits the class that implements the handlerinterceptor interface, such as the abstract class handlerinterceptoradapter that implements the handlerinterceptor interface provided by spring; The second way is to implement the webrequestinterceptor interface of spring, or inherit the class that implements webrequestinterceptor.
(1) Prehandle (HttpServletRequest request, httpservletresponse, object handle) method, which will be called before request processing. Interceptors in spring MVC are chain calls. Multiple interceptors can exist simultaneously in an application or in a request. Each interceptor call will be executed in sequence according to its declaration order, and the first execution is the prehandle method in the interceptor. Therefore, some pre initialization operations or preprocessing of the current request can be carried out in this method, or some judgments can be made in this method to determine whether the request should continue. The return value of this method is of boolean type. When it returns false, it means that the request ends and subsequent interceptors and controllers will not execute again; When the return value is true, it will continue to call the prehandle method of the next interceptor. If it is the last interceptor, it will call the controller method of the current request.
(2) The posthandle (HttpServletRequest request, object handle, modelandview, modelandview) method is explained by the prehandle method. We know that this method, including the aftercompletion method to be mentioned later, can only be called when the return value of the prehandle method of the current interceptor is true. As the name suggests, the posthandle method is executed after the current request is processed, that is, after the controller method is called, but it will be called before the dispatcher servlet renders the view return, so we can operate the modelandview object after the controller processing in this method. The posthandle method is called in the opposite direction to the prehandle, that is, the posthandle method of the interceptor declared first will be executed later.
(3) Aftercompletion (HttpServletRequest request, exception Ex) method, which is also executed only when the return value of the prehandle method of the current corresponding interceptor is true. As the name suggests, this method will be executed after the end of the entire request, that is, after the dispatcher servlet renders the corresponding view. The main function of this method is to clean up resources. The order of execution is as follows:
It should be noted that only when prehandler is true can posthandler be executed. When it is false, aftercompletion will still execute.
Summary of differences between filter and interceptor
1. Filter interface in javax Under the servlet package. Intelliceptor is defined at org springframework. web. In the servlet. 2. Filter is specified by servlet, and interceptor can be used in web program or application. 3. Filter is supported by servlet container and interceptor is supported by spring framework. 4. Filter is released through dochain, and interceptor is released through prehandler. 5. Filter is only executed before and after the method. The granularity of interceptor is finer. It can go deep into before and after the method and before and after the exception is thrown.