Java filter, a sequential point relationship between SPRINGMVC interceptors

Source: Internet
Author: User

As a part of the recent project is the interface remote call, the use of access rights and business permissions of the authentication, need to use SPRINGMVC interceptor, used to use Struts2 before the interceptor, and SPRINGMVC interceptor function has not been studied before, so this time to a little research, The conclusion is that the SPRINGMVC interceptor and the Struts2 interceptor principle are almost exactly the same, all using the reflection function to realize the dynamic proxy.

Because filters and interceptors have a lot of similarities and even the same things, because most of the time they can achieve the same ability. So I looked at the filter again.

The difference between filters and interceptors, Baidu a bit:

① 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, interceptors can be called multiple times, and filters can only be called once when the container is initialized

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    publicvoidthrows  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 BooleanPrehandle (HttpServletRequest request, httpservletresponse response, Object handler)throwsException {System.out.println ("Prehandle"); return true; } @Override Public voidPosthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview Modelandview)throwsException {System.out.println ("Posthandle"); } @Override Public voidAftercompletion (HttpServletRequest request, httpservletresponse response, Object handler, Exception ex)throwsException {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.

Refer to this post for a moment: http://haohaoxuexi.iteye.com/blog/1750680

Java filter, a sequential point relationship between SPRINGMVC interceptors

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.