The relationship and difference between Java filter and SPRINGMVC interceptor

Source: Internet
Author: User

The difference between filters and interceptors:

  ① interceptors are based on the reflection mechanism of Java, and filters are based on function callbacks.
The ② interceptor is not dependent on the servlet container, and the filter relies on the servlet container.
The ③ interceptor only works on action requests, while filters can work on almost all requests.
The ④ interceptor can access the action context, the object in the value stack, and the filter cannot be accessed.
⑤ 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.

It is important that the ⑥ interceptor can get the individual beans in the IOC container, and that the filter is not, which is a matter of injecting a service into the interceptor to invoke the business logic.

Write a bit of test code, by the way, to figure out the sequence of these people:

1. Filter is Java EE standard, using function callback method. is pre-processing after the request enters the container, before it enters the servlet, and after the request is returned to the front end.

 
    @Override public    void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {        System.out.println ("before ...");        Chain.dofilter (request, response);        System.out.println ("After ...");    
 

Chain.dofilter (request, response); The invocation of this method serves as a watershed. In fact, the Doservice () method of calling the servlet is in Chain.dofilter (request, response), which is done in this method.

2. The interceptor is wrapped in a filter.

 
    @Override Public    Boolean prehandle (httpservletrequest request, httpservletresponse response, Object handler) Throws Exception {        System.out.println ("Prehandle");        return true;    }    @Override public    void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview) throws Exception {        System.out.println ("Posthandle");    }    @Override public    void Aftercompletion (HttpServletRequest request, httpservletresponse response, Object handler, Exception ex) throws Exception {        System.out.println ("aftercompletion");    }
 

A.prehandle () This method is performed in the previous step of the Chain.dofilter (request, Response) method of the filter, i.e. in the [System.out.println ("Before ...")] [ Executed between Chain.dofilter (request, response)].

B.prehandle () method, the Modelandview content of the controller can be manipulated before the return Modelandview.

The C.aftercompletion () method is performed in the previous step of the filter return to the front end, that is, in [Chain.dofilter (Request, Response)][system.out.println ("After ...")] executed between them.

The 3.SpringMVC mechanism is to distribute requests to different controllers by the same servlet, in fact this step is performed in the servlet's service () method. So the filter, Interceptor, Service () method, the execution order of the DISPATC () method should be like this, roughly draw a picture: Actually very good test, write a filter, an interceptor, and then in these methods are added a breakpoint, all the way down the F8 came to a conclusion.

  

  Summary: The interceptor work is really useful in the authentication of the request authority, in the project I am involved in, the third party's remote invocation of each request needs to participate in the authentication, so it is very convenient, and he is very independent logic, so that the business logic code is very clean. And the other functions of the framework, the principle is very simple, it is very simple to use, a general look at the next SPRINGMVC this part of the source code, in fact, it is relatively easy to understand.

We only used the Prehandle method in our project, but not the other, the framework provides an adapter class Handlerinterceptoradapter that has implemented the Interceptor interface, inherits the class and then rewrites the method that needs to be used, can be a few lines of code, This approach is manifested in many places in Java.

The above section is an excerpt from the God-like blog, referring to this post: http://haohaoxuexi.iteye.com/blog/1750680

You can also refer to this ebook:

The relationship and difference between Java filter and SPRINGMVC interceptor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.