Home > Java > javaTutorial > What does filter mean?

What does filter mean?

(*-*)浩
Release: 2020-09-15 15:51:00
Original
46279 people have browsed it

Filter means "filter", which is a filter between the client and the server resource file. Before accessing the resource file, the request is modified and judged through a series of filters. Intercept or modify requests that do not comply with the rules in the middle; you can also filter, intercept or modify responses.

What does filter mean?

Filter, as the name suggests, is the filtering and preprocessing process of data. Why introduce filters? When visiting a website, sometimes some sensitive information is sent, and after it is displayed, the sensitive information will be replaced with * and other characters. This means that the information is processed by a filter.

Recommended course: Java Tutorial.

This is just a simple example. Of course, the filter is so powerful, and its function cannot be limited to this. It can not only preprocess data, but also preprocess requests as long as they are sent. , at the same time, it can also preprocess the response returned by the server, thus greatly reducing the pressure on the server. For example, it implements some advanced functions such as URL-level permission access control, filtering sensitive words, and compressing response information. Let’s take a closer look at filters.

Filter technology is a newly added feature of servlet 2.3. Servlet2.3 was released by Sun in October 2000. Its developers include many individuals and corporate groups, which fully reflects the code openness principle advocated by Sun. With the joint efforts of many participants, servlet2.3 is much more powerful than before, and its performance has also been greatly improved.

1. Concept

Filtering function filters requests sent from the client to the server, and can also process the response returned by the server. It enables users to change a request and modify a response. Filter is not a servlet, it cannot generate a response, but it can preprocess a request before it reaches the servlet, and it can also process the response when the response leaves the servlet. In other words, filter is actually a transmitter between the client and servlet, and it can modify what is to be passed.

Note: Filters are used to intercept requests and responses and cannot generate responses, while servlets are used to process requests and generate responses.

2. Applicable occasions

Realize URL-level permission access control, filter sensitive words, compress response information, etc.

3. How the filter implements interception

When the client makes a request, the filter intercepts the client's HttpServletRequest before the HttpServletRequest reaches the Servlet. Check HttpServletRequest as needed, and you can also modify the HttpServletRequest header and data. Call the doFilter method in the filter to release the request. After the request reaches the Servlet, the request is processed and an HttpServletResponse is generated and sent to the client. The filter intercepts the HttpServletResponse before it reaches the client. Check HttpServletResponse as needed and modify the HttpServletResponse header and data. Finally, the HttpServletResponse reaches the client.

4.Filter interface

Servlet API provides a Filter interface, and the filter written must implement this interface.

5.Filter life cycle

(1) There are three important methods in the Filter interface.

init() method: initialization parameters, automatically called when creating a Filter. When we need to set initialization parameters, we can write it in this method. doFilter() method: doFilter will be executed when a request to be executed is intercepted. Here we write our preprocessing of requests and responses. destroy() method: automatically called when destroying the Filter.

(2) Filter life cycle

The creation and destruction of Filter is controlled by the web server.

When the server starts, the web server creates an instance object of Filter and calls its init method to complete the initialization function of the object. The filter object will only be created once, and the init method will only be executed once. When a request is intercepted, the doFilter method is executed. Can be executed multiple times. When the server shuts down, the web server destroys the Filter instance object.

6.Filter object——FilterConfig

When configuring the filter, the user can use to configure some initialization parameters for the filter. When the web container instance Filter object, when calling its init method, the filterConfig object encapsulating the filter initialization parameters will be passed in. Therefore, when developers write filters, they can obtain the name of the filter through the method of the filterConfig object:

String getFilterName(): Get the name of the filter. String getInitParameter(String name): Returns the value of the initialization parameter with the name specified in the deployment description. If it does not exist, return null.Enumeration getInitParameterNames(): Returns an enumeration collection of the names of all initialization parameters of the filter. public ServletContext getServletContext(): Returns a reference to the Servlet context object.

7. Filter Chain——FilterChain

A group of filters intercepts certain web resources, then this group of filters is called a filter chain. The execution order of filters is related to (whoever executes first).


The above is the detailed content of What does filter mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template