【Filter】

Main page access control

Technical knowledge points involved

filter

filter

  1. For WEB applications, the filter is a WEB component that resides in the server. It can intercept the request and response information between the client and the WEB resource. WEB resources may include Servlet, JSP, HTML pages, etc.

  2. When the server receives a specific request, it will first hand the request to the filter. The programmer can read and modify the request information in the filter, and then send the request information to the target resource. After the target resource responds, the server will forward the response to the filter again. In the filter, you can also do some operations on the response information, and then send the response to the server.

  3. That is to say, the filter can do some corresponding operations on the request and response information before the web resource receives the request and the browser receives the response.

  4. Multiple filters can be deployed in a WEB application. Multiple filters form a filter chain. The request and response must pass through multiple filters before reaching the target.

Insert picture description here

  1. After the browser request is processed by filters 1, 2, and 3, it enters the servlet processing. After the processing is completed, it returns to the browser in the same way, that is, it returns from 3, 2, 1 after coming out of the servlet.

Use of filters

  1. Complete filter development by implementing the Filter interface
@WebFilter(filterName = "LoginFilter",urlPatterns = "/*")
public class LoginFilter implements Filter {
    
    
    public void destroy() {
    
    
        System.out.println("LoginFilter 过滤器摧毁了......");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
    
    

    }

    public void init(FilterConfig config) throws ServletException {
    
    
        System.out.println("LoginFilter 过滤器初始化了......");
    }

}
  1. Filter can be configured in the annotation, as in the above code; it can also be configured in the xml file. as follows:

Insert picture description here

  1. Here I want to enter the main.jsp page only if the login.jsp page is successfully logged in, otherwise it cannot enter the main.jsp directly, then the main.jsp page needs to be filtered, so url=pattern can be written as /main.jsp, Filling in /* here is to filter out all the interfaces. The filter conditions can be implemented in the filter class. The filter conditions are actually implemented in doFilter. The code is as follows.
@WebFilter(filterName = "LoginFilter",urlPatterns = "/*")
public class LoginFilter implements Filter {
    
    
    public void destroy() {
    
    
        /*初始化成功在控制台输出*/
        System.out.println("LoginFilter 过滤器摧毁了......");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
    
    
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        System.out.println("URI:"+request.getRequestURI());
        System.out.println("URL:"+request.getRequestURL());
        String url = request.getRequestURL().toString();
        /*判断过滤的要求*/
        if(url.endsWith("/main.jsp")){
    
    
            /*如果没有登陆就想直接进入主页面,那么就过滤跳转到登录界面*/
            response.sendRedirect("login.jsp");
        }else{
    
    
            /*如果直接进入的不是主页面,那么就放行*/
            chain.doFilter(request, response);/*保证使用的是同一个request,response对象*/
        }

    }

    public void init(FilterConfig config) throws ServletException {
    
    
        /*销毁之前在控制台输出*/
        System.out.println("LoginFilter 过滤器初始化了......");
    }

}

In this way, the filter condition is realized.

Main page access control requirements

  1. The login status must be judged when entering the main page. If you are not logged in, you are not allowed to enter the main interface.

  2. The judgment of the login status is implemented in the filter, which is more versatile and pluggable.

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/109450878