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.*;


public class CharacterEncodingFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter Initialized!");

    //Filter to do
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        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.
        System.out.println("CharacterEncodingFilter After execution......");

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


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.


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;


public class ShowServlet extends HttpServlet {

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

    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.



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