Today is the 93rd day for Liu Xiaoai to learn Java by herself.

Thank you for watching. Thank you.

Without further ado, let’s begin today’s lesson:


I. Overview of Filter

Filters in javase

When learning JavaSE, I have been exposed to file filters. There is an interface FileFilter in JDK.

Using the File class for interface programming can play a filtering effect on corresponding files.

Because I was new to the idea of interface programming, I studied it in detail.

The result was a frenzy of diss, several of which mentioned filters on the Web.


Today, 53 days later, I finally got here.

Take a closer look at this web filter in the next two days and see what’s really cool about it.

2 Filters on the Web

Filter indicates that resources accessed by clients are filtered on the Web. If the conditions are met, the filtering is not.

In Java, there must be a class or interface that corresponds to it, because of its rule-making function, in this case the interface.

Without further words, directly look at the Filter interface source:


(1) the init () method

The meaning of initialization, which has a parameter FilterConfig, is also easy to understand, initialization requires configuration information.

(2) the doFilter () method

There is no doubt that the core method is this method.

So how do you filter, of course, is just rewriting this method.

(3) destroy () method

Destroy means, needless to say, this method is called when the server stops running.

Two, filter two ways to achieve

Like servlets, filters can be implemented as XML configuration files and annotation development.

1xml configuration file


1) web. XML

This is the same as the servlet configuration, except that the label name is filter.

  • Filter-name is determined by yourself. Ensure that the two filter-names are the same.
  • Filter-class is the class name and package name of a user-defined class.
  • Url-pattern indicates the mapping path, which corresponds to the browser address.

② Create a custom FilterOne class

Implement the Filter interface to achieve a filtering effect: the mapping path in the web.xml configuration information is filtered.

FilterChain, which is also a Java interface, the source code is as follows:


This interface is a parameter in the doFilter() method of the Filter interface, which also has a method doFilter().

Be careful not to confuse the two doFilter() methods.

The filterchain-dofilter () code is the same as the pass code. If this code is present, the filter will not work.

The FilterChain interface is explained in Point 3.

(3) test

Create two files: filterone.html and filtertwo.html, where filterone.html corresponds to the mapping path in web.xml.

  • When the browser visits filterone.html, there is no output and the file is filtered.
  • When the browser visits filtertne.html, there is output and the file is not filtered.

Annotation implementation


The steps are the same as for servlets:

Right-click New and select Filter to achieve an annotation development effect.

The convenience is that you don’t have to fill in the configuration information in web.xml; everything else is the same.


The doFilter() method of FilterChain

  • If doFilter() is not written, the file cannot be accessed and is blocked.
  • Write the doFilter() method, the file is accessible and allowed.
  • Therefore, doFilter() has the function of release.

Three, the implementation process of the filter

The same path cannot correspond to multiple servlets. Can the same path correspond to multiple filters?

The answer is yes, in fact, it is easy to understand, is the judgment of multiple filter conditions to get a result.

What about their execution process?

You need to understand the FilterChain interface and its doFilter() function. Write code to do a test:


① Three filters

I created three filters, FilterA, FilterB, and FilterC. The figure above shows only one filter.

Write two output statements in the code to determine the order of execution, bounded by the doFilter() method of FilterChain.

② Filter the resources to be filtered

I used an HTML file as a filtered resource, but a Servlet can also be a filtered resource.

The path is the same as the path of the three filters.

Code test:


The execution order is alphabetical, so FilterA comes first.

As for understanding FilterChain:

The Chain itself means a Chain, bounded by the doFilter() method of the FilterChain, which behaves like a Chain.

Is it easy to understand if you draw a picture like this?

The last

Thanks for watching.

If you can, please give it a thumbs up. Thank you.

This article is formatted using MDNICE