A note on Java filters

Everyone knows that there are filter interceptors and listeners in java. The functions of filters and interceptors are very similar. As a result, beginners often don't know which one to use, so let's introduce the difference between the two first.

The difference between Filter and Interceptor:

1. The filter is based on function callbacks, and the interceptor is based on the java reflection mechanism;

2. The filter is specified by the servlet specification and can only be used in web programs, while the interceptor is in the spring container, which does not depend on the servlet container

3. The filter can intercept almost all requests (including requests for static resources), while the interceptor only intercepts action requests (does not intercept static resource requests)

4. Filters cannot access the objects in the action context and value stack, while interceptors are all possible.

5. Interceptors can obtain objects in the spring container, but filters cannot

6. The interceptor can be called multiple times during the life cycle of the action, while the filter is only called once when the container is initialized.

7. The interceptor is wrapped in a filter.

Filters actually intercept web resources, do some processing, and then pass them to the next filter or servlet for processing.
Usually they are used to intercept the request for processing, and the returned response can also be intercepted.

The entire request process is as follows (here quoted from http://blog.csdn.net/chenleixing/article/details/44573495 blog pictures):
Insert picture description here

The specific steps to configure filters in java
First create a Filter folder and create a class type LoginFilter.class in it
Insert picture description here

Inherit the Filter interface and reconstruct the doFilter method

public class LoginFilter implements Filter {
    
    

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    
        // 过滤器出生
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    
    
        //强转
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        if (session.getAttribute("userInfo")!=null && request.getRequestURL().indexOf("/userInfoController/doLogin.do")!=-1){
    
    

                //登陆成功
            filterChain.doFilter(request,response);
            }else {
    
    
            response.sendRedirect(request.getContextPath()+"//userInfoController/doLogin.do");
        }
    }

    @Override
    public void destroy() {
    
    
        //过滤器销毁
    }
}

Because the filter exists in the servlet, it is configured in web.xml.

<!--使用filter过滤器-->
  <filter>
    <filter-name>SessionFilter</filter-name>
    <filter-class>com.zrgj.filter.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>SessionFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

  使用springSecurity过滤器
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Guess you like

Origin blog.csdn.net/hzl529/article/details/102667386