Interceptor
Main role: Interception of user requests, processing, such as the determination of user login, authorization authentication, mainly for the action request processing. is achieved through Handlerinterceptor.
The configuration is as follows:
<mvc:interceptors> <bean class= "Cn.appsys.testInterceptor" ></bean>//intercept all requests <MVC: interceptor> <mvc:mapping path= "/manager/backend/**"/> <bean class= " Cn.appsys.interceptor.SysInterceptor "/>//intercept above request </mvc:interceptor> </mvc:interceptors>
Generic interceptors can be implemented by implementing Handlerinterceptor interfaces or inheriting handlerinterceptoradapter. The code is as follows:
public class Testinterceptor implements Handlerinterceptor { @Override public boolean Prehandle ( HttpServletRequest HttpServletRequest, HttpServletResponse httpservletresponse, Object o) throws Exception { System.out.println ("Prehandle"); return true; } @Override public void Posthandle (HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse , Object O, Modelandview modelandview) throws Exception { System.out.println ("Posthandle"); } @Override public void Aftercompletion (HttpServletRequest httpservletrequest, HttpServletResponse HttpServletResponse, Object O, Exception e) throws Exception { System.out.println ("Aftercompletion");} }
Prehandle is implemented before the request arrives controller, can perform the user check login and so on, returns true, the request arrives at the controller layer, the Posthandle method is after executing controller layer code, Dispatcherservlet before rendering the view
execution, so the Modelandview object can be processed, and the Aftercompletion method is to execute the call after the Dispatcherservlet renders the view, mainly to do some cleanup of resources.
Note: Only the controller request can be intercepted and some static resources cannot be intercepted.
Filter
Main role: Set the character set and so on uniformly.
Depending on the servlet container, the filter instance is called only once at initialization time.
The filter is configured in Web. Xml as follows:
General filters can be implemented by implementing the filter interface. The code is as follows:
public class Testfilter implements filter { @Override public Void Destroy () { System.out.println ("Filter Destroy "); } @Override public void DoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2) throws IOException , servletexception { System.out.println ("Filter doFilter before"); Arg2.dofilter (arg0, arg1); System.out.println ("Filter doFilter after"); } @Override public void init (Filterconfig arg0) throws Servletexception { System.out.println ("Filter init"); c16/>}}
Interceptor and filter Execution order:
1, Filter.init ();
2, Filter.dofilter (); Before DoFilter
3, Handlerinterceptor.prehandle ();
4. Controller method Execution
5, Handlerinterceptor.posthandle ();
6. Dispatcherservlet View Rendering
7, Handlerinterceptor.aftercompletion ();
8, Filter.dofilter (); After DoFilter
9, Filter.destroy ();
The difference between filter and interceptor
- Filter is based on the function callback (DoFilter () method), while interceptor is based on Java reflection (AOP thought).
- The filter relies on the servlet container, and interceptor does not depend on the servlet container.
- Filter works on almost all requests, and interceptor only works on action requests.
- Interceptor can access the context of the action, the object in the value stack, and filter cannot.
- In the life cycle of an action, interceptor can be called multiple times, and filter can only be called once when the container is initialized.
Interceptor and filter differences in SPRINGMVC