Filter (easy to understand)

I. Introduction

Filter: Filter, used to filter website data (for example, when we play games, we will curse people, they will become ***)

  • Handling Chinese garbled characters
  • Login verification

Insert picture description here

2. Filter development steps:

  1. Guide package
<dependencies>
        <!--Servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <!--JSP依赖-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>

        <!--JSTL依赖-->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>

        <!--standard标签库依赖-->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!--连接数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
  1. Write filter
package com.xu.filter;

import javax.servlet.*;  //实现Filter接口时,导包不要导错,要java.servlet包
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
    
    
    //初始化:web服务器启动就已经初始化了,随时等待过滤对象出现!
    public void init(FilterConfig filterConfig) throws ServletException {
    
    
        System.out.println("CharacterEncodingFilter初始化!");
    }

    /*
    1.过滤中的所有代码,在过滤特定请求的时候都会执行
    2.必须要让过滤器继续同行
      chain.doFilter(request,response);
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
    
        //处理编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("CharacterEncodingFilter执行前...");

        chain.doFilter(request,response);   //让我们的请求继续走,如果不写,程序到这里就被拦截停止了

        System.out.println("CharacterEncodingFilter执行后...");

    }

    //销毁:web服务器关闭的时候,过滤器会销毁
    public void destroy() {
    
    
        System.out.println("CharacterEncodingFilter销毁!");
    }
}

  1. Configure Filter in web.xml (similar to configuring servlet resource path)
<!--配置Filter-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.xu.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <!--只要是/servlet的任何请求,都会经过这个过滤器-->
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>

3. Explain the above process in vernacular

  1. We first create a class that inherits the HttpServlet class (create a servlet program), the purpose of the code is to output Chinese to the page, but there will be garbled problems:
public class ShowServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter().write("你好,世界");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        doGet(req,resp);
    }
}

Insert picture description here

  1. At this point, we need to create a filter and configure it in the web.xml file: any request for /servlet will pass this filter . So we can solve the garbled problem by outputting localhost:8080/servlet/show

Insert picture description here
Note: This part of the content is to help Xiaobai explain and explain, the focus is on the second section, the Filter filter is really very simple, just a few times to master it.

Guess you like

Origin blog.csdn.net/weixin_46594796/article/details/109545126