Discussion on the difference between Interceptor and Filter in SpringMVC
- 2021-07-16 02:18:33
- OfStack
Interceptor
Main role: Intercept user requests, processing, such as judging user login, authority verification, as long as the Controller request for processing, is through HandlerInterceptor.
Interceptor is divided into two situations, one is to intercept the session, implement the HandlerInterceptor interface of spring and register it in the interception queue of mvc, in which preHandle () method intercepts before calling Handler (step 3 above), postHandle () method calls before rendering the view (step 5 above), and afterCompletion () method executes before returning to the corresponding one; The other is method interception, which requires the @ Aspect annotation to intercept before and after each call to the specified method.
Filter
Main function: Filter character code, make 1 business logic judgment, mainly used to preprocess user request, and also make logic judgment.
Filter is filtered by filter before it requests to enter the servlet container to execute the service () method (step 1 above). Unlike Intreceptor1, which relies on the springmvc framework, it only needs to rely on servlet. Filter startup is started with the startup of WEB application, which only needs to be initialized once, and can be intercepted in the future.
There are several categories of Filter:
User authorization Filter: Check the user request and filter the illegal user request according to the request; Log Filter: Record some special user requests; Decoding Filter: Decoding the request for non-standard encoding.
Differences between Filter and Interceptor
Interceptor
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 the above request
</mvc:interceptor>
</mvc:interceptors>
1 interceptor can be implemented by implementing HandlerInterceptor interface 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 at Controller, and can perform user verification login and other operations. After returning to true, the request arrives at Controller layer; The postHandle method is done after Controller layer code is executed and before DispatcherServlet renders the view
Execute, so ModelAndView objects can be processed; afterCompletion method is in DispatcherServlet to render the view after the execution of the call, mainly for a resource cleaning and other work.
Note: Only Controller requests can be intercepted, but some static resources cannot be intercepted.
Filter
Main functions: Set the character set in Unification 1, etc.
Depending on the servlet container, the filter instance is called only once at initialization time.
The filter is configured in web. xml as follows:
<filter>
<filter-name>testFilter</filter-name>
<filter-class>cn.appsys.TestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>testFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1 general filter can be implemented by implementing 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");
}
}
Interceptor and filter execution sequence:
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 ();