The difference between Java filters and interceptors

Last night dreamed interviewer asked me the difference between filters and interceptors. . .

The difference between the filter and the interceptor:
1, and a filter interceptor Trigger timing is not the same, the filter is pre-treated prior to the request to enter the container, but the request into the servlet.
End of the request is returned, the servlet process is complete, prior to return to the front end.
2, the interceptor can acquire IOC container respective the bean, and the filter will not work, because the interceptor is spring provided and managed,
spring functions can be used interceptor, inject a service interceptor and can call the business logic .
JavaEE standard and the filter is simply dependent servlet api, does not depend on spring.
3, implement filters based on callback. Implemented interceptor (proxy mode) based on reflection, the proxy agent static and dynamic sub-agents,
the dynamic proxy is a simple implementation of the interceptor.

When to use interceptors? When to use a filter?
If the item is non-spring, then the interceptor can not be used, only use filters.
If the front is a process controller, may be used interceptors filters may also be used.
If it is before and after treatment dispaterServlet, only using a filter.

Filter annotation to achieve:

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author maxixnhai
 * @PackageName www.maxinhai.com.diary.common.filter
 * @ClassName AuthFilter
 * @Description
 * @date 2020/4/5 17:41
 */
@WebFilter(urlPatterns = "/*", filterName = "authFilter")
public class AuthFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long start = System.currentTimeMillis();
        Object name = request.getParameter("name");
        System.out.println("name => " + name);
        for(int i=0; i<10000; i++) {
            //System.out.println("过滤器打印: " + i);
        }
        long end = System.currentTimeMillis();
        System.out.println("authFilter 耗时: " + (end - start));
    }

    @Override
    public void destroy() {

    }

}

Notes implementation requires scanning filter starter (This article SpringBoot) Add @ServletComponentScan ( "www.maxinhai.com.diary.common").

Non-annotated true, we need to add the following configuration in web.xml:

<!-- 配置登陆过滤器 -->
    <filter>
        <filter-name>authFilter</filter-name>
        <filter-class>www.maxinhai.com.filter.AuthFilter</filter-class>
        <init-param>
            <param-name>passUrl</param-name>
            <param-value>index;FunctionServlet</param-value>
           </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>loginFilter</filter-name>
           <url-pattern>/*</url-pattern>
    </filter-mapping>

 

Guess you like

Origin www.cnblogs.com/mxh-java/p/12638455.html