Java filter, SpringMVC interceptor between a sequential relationship, the filter springmvc
Recently, some of the projects involve remote interface calls and access permission and Service permission identification. SpringMVC interceptor is required. Previously, Struts2 was used as an interceptor, springMVC's interceptor function has not been studied before, so this time I will study it a little bit. The conclusion is that SpringMVC's interceptor and Struts2 have almost the same principle, all use the reflection function to implement dynamic proxy.
Because filters and interceptors have many similarities and even similarities, because they can achieve the same capabilities in most cases. So I read the filter again.
The difference between filters and interceptors is Baidu's:
① The Interceptor is based on the java reflection mechanism, while the filter is based on function callback.
② The interceptor does not depend on the servlet container, and the filter depends on the servlet container.
③ The interceptor can only act on action requests, while the filter can act on almost all requests.
④ The interceptor can access objects in the action context and value stack, while the filter cannot.
⑤ In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.
I wrote some test code. By the way, I sorted out my ideas and figured out the order between them:
1. The filter is a Java EE standard and implemented using function callback. Preprocessing is performed after the request enters the container and before it enters the Servlet, and the post-processing is performed between the end of the request and the frontend.
@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 call of this method serves as a watershed. In fact, the Servlet doService () method is called in the chain. doFilter (request, response); method.
2. the interceptor is enclosed in the 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 () is used in the chain of the filter. the doFilter (request, response) method is executed in the previous step, that is, in the [System. out. println ("before... ")] [chain. executed between doFilter (request, response.
B. After the preHandle () method, you can control the ModelAndView content of the Controller before returning ModelAndView.
C. the afterCompletion () method is executed one step before the filter is returned to the front-end, that is, in the [chain. doFilter (request, response)] [System. out. println ("after....
3. SpringMVC uses the same Servlet to distribute requests to different controllers. In fact, this step is executed in the Servlet service () method. Therefore, the execution sequence of the filters, interceptors, and service () methods and dispatc () methods should be like this. We have drawn a rough picture: in fact, it is very good to test and write a filter by yourself, an interceptor, and then add a breakpoint in these methods. The f8.
Summary: Interceptor functions are indeed very useful in the authentication of request permissions. In the project I participated in, every request for remote calls by a third party needs to be authenticated, so this is very convenient, in addition, it is a very independent logic, so that the business logic code is very clean. Like other functions of the framework, the principle is very simple and easy to use. It is easy to understand the source code of SpringMVC.
The preHandle method is only used in our project, but nothing else is used. The framework provides an adapter class HandlerInterceptorAdapter that has implemented the interceptor interface, inherit this class and rewrite the method that needs to be used. A few lines of code are allowed. This method is embodied in many aspects of Java.
Refer to this post: http://haohaoxuexi.iteye.com/blog/1750680