JavaWeb 22 Filter

7.2 Filter

Filter: filter for filtering website data.

  • For example, deal with Chinese garbled code
  • validate logon

The filter is also a program in the background, just to implement the filter interface.

The first thing we need to know is that we must not import the wrong interface.
The Filter interface supported by Tomcat 10 is under jakarta.

Then we will find that we need to rewrite three methods to initialize filter destruction

  • Initialization is usually about what needs to be done about the data before the servlet program is actually executed.

  • Filtering is generally the processing of filtering before the servlet program is executed, such as login verification, coding transformation, and so on..

  • Destruction is what to do when destroying!

Then we can make a demonstration. For example, in the past, when we dealt with the problem of Chinese garbled code, we wrote it in each servlet program. This time, we wrote it directly in the Filter program.

package com.muquanyu.filter;

import jakarta.servlet.*;

import java.io.IOException;

public class CharacterEncodingFilter implements Filter {

    //initialization
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter Initialized!");
        //Filter.super.init(filterConfig);
    }

    //Filter to do
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html");
        servletResponse.setCharacterEncoding("utf-8");

        System.out.println("CharacterEncodingFilter Before execution......");

        //Chain chain
        //Let our request go on. If we don't write the program, it will be intercepted and terminated here.
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("CharacterEncodingFilter After execution......");
    }

    //Destroy: the filter will not be destroyed until the web server is shut down!
    @Override
    public void destroy() {
        System.out.println("CharacterEncodingFilter Destroyed!");
        //Filter.super.destroy();
    }
}

filterChain.doFilter(servletRequest,servletResponse);

What does this code do. It means chaining, that is, after we filter one, if you want to continue filtering, you must call this code. Because we know that every request is different. We must have a way to receive different requests and responses every time!!

In this way, the request and response can be filtered again! So this code must be written! If you don't write, the program will terminate here... This is a usage rule.

After we have finished writing the filter code, can we use it directly?

Should we declare which request addresses the filter applies to? Only by making this declaration can our filter know where to use. Otherwise, it will be written in vain.

So the filter is on the web XML should also write its own configuration.

<filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.muquanyu.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>

This means that the filter is applied to all request addresses under / Servlet /. As long as it is related to this address, we must first go through the filter to process the request, response and even some data in advance. Then you can execute the Servlet program.

  • Servlet Chinese garbled program
package com.muquanyu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class ShowServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("I am Chinese!");
        //super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doPost(req, resp);
    }
}
  • In order to distinguish whether the filter works or not, we set the servlet program to two url requests.
<servlet>
        <servlet-name>ShowServlet</servlet-name>
        <servlet-class>com.muquanyu.servlet.ShowServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ShowServlet</servlet-name>
        <url-pattern>/servlet/show</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>ShowServlet</servlet-name>
        <url-pattern>/show</url-pattern>
    </servlet-mapping>

One is / show and the other is / servlet/show

We know that the filter we write will only filter the response of servlet/show, not show, so when accessing show, the Chinese garbled code should be displayed, while the servlet/show processed by the filter is displayed to us in normal Chinese!!!



When the server is turned off, the method of destruction is automatically executed.

In this way, the Filter filter is just so. It seems to be similar to a public preprocessor to deal with various problems in advance.

Keywords: Java servlet filter

Added by phynias on Thu, 23 Dec 2021 12:42:04 +0200