1. Servlet filter concept:
◆ Servlet filter is defined in Java Servlet specification 2.3. It can check and modify the request and response objects of Servlet containers.
◆ Servlet filters do not generate request and response objects. They can only be used for filtering. Servlet expiration can check the Request object before the Servlet is called, modify the Request Header and Request content, check the Response object after the Servlet is called, and modify the Response Header and Response content.
The Web components used for Servlet expiration filtering can be Servlet, JSP, or HTML files.
Ii. Servlet filter features:
◆ Servlet filters can be used to check and modify ServletRequest and ServletResponse objects.
◆ The Servlet filter can be specified to be associated with a specific URL. The filter is triggered only when the customer requests to access the URL.
◆ Servlet filters can be connected together to form a pipeline effect, collaboratively modifying request and response objects
Iii. Functions of Servlet filters:
◆ Query the request and make corresponding actions.
◆ Block the request-response pair so that it cannot be further transmitted.
◆ Modify the request header and data. You can provide custom requests.
◆ Modify the response header and data. Users can achieve this by providing custom response versions.
◆ Interact with external resources.
Iv. application scenarios of Servlet filters:
◆ Authentication Filtering
◆ Login and Audit filtering
◆ Image conversion and filtering
◆ Data compression and filtering
◆ Encrypted Filtering
◆ Token Filtering
◆ Resource Access trigger Event Filtering
◆ XSL/T Filtering
◆ Mime-type filtering
5. Servlet filter interface composition:
All Servlet Filter classes must implement the javax. servlet. Filter interface. This interface contains three methods that must be implemented by the filter class:
◆ Init (FilterConfig ):
This is the initialization method of the Servlet filter. This method will be called after the Servlet container creates the Servlet filter instance. In this method, you can read the Servlet filter initialization parameters in the web. xml file.
◆ DoFilter (ServletRequest, ServletResponse, FilterChain ):
This method completes the actual filtering operation. When the customer requests to access the URL associated with the filter, the Servlet container will first call the doFilter method of the filter. FilterChain parameters are used to access subsequent filters.
◆ Destroy ():
The Servlet container calls this method before destroying the filter instance. In this method, resources occupied by the Servlet filter can be released.
6. Create a Servlet filter:
◆ Implement the javax. servlet. Filter Interface
◆ Implement the init method and read the initialization function of the filter.
◆ Implement the doFilter method to respond to requests or filters
◆ Call the doFilter method of the FilterChain interface object to pass requests or responses to subsequent Filters
◆ Destroy the filter
7. Servlet filter:
◆ Create a filter instance for the Servlet container
◆ The filter instance calls the init method to read the initialization parameters of the filter.
◆ The filter instance calls the doFilter method and determines whether the request is valid based on the value of the initialization parameter.
◆ If the request is not properly blocked
◆ If the request is valid, call the chain. doFilter method to send the request to the future
8. Servlet filter:
◆ The filter intercepts client requests
◆ Re-encapsulate ServletResponse to provide custom output streams in the encapsulated ServletResponse
◆ Send requests to the future
◆ Response from Web Components
◆ Obtain the custom output stream from the encapsulated ServletResponse
◆ Write the response content to the buffer stream through the custom output stream
◆ Modify the response content in the buffer stream, clear the buffer stream, and output the response content
9. Servlet filter release:
◆ When publishing a Servlet filter, you must add the <filter> element and <filter-mapping> element to the web. xml file.
◆ The filter element is used to define a filter:
- // Attribute meaning
- Filter-name: Specifies the name of the filter.
- Filter-class specifies the class Name of the filter.
- Init-param provides initialization parameters for the filter instance, which can contain multiple
◆ The filter-mapping element is used to associate a filter with a URL:
- // Attribute meaning
- Filter-name: Specifies the name of the filter.
- Url-pattern specifies the URL associated with the filter. "/*" indicates all URLs.
11. Precautions for using Servlet Filters
◆ Because Filter, FilterConfig, and FilterChain are all located in javax. in the servlet package, the requests and response objects ServletRequest and ServletResponse used in the HTTP package must be converted to HttpServletRequest and HttpServletResponse before subsequent operations.
◆ When configuring Servlet and Servlet filters in web. xml, the filter element should be declared first, and then the Servlet element should be declared.
◆ To observe the log generated by the filter in the Servlet, make sure that the following <logger> element is configured in the
- ﹤Logger className = “org.apache.catalina.logger.FileLogger”
- directory = “logs”prefix = “localhost_log.”suffix=”.txt”
- timestamp = “true”/﹥
The Servlet filter is introduced here. Do you know about Servlet filters?
- What are servlets and common Servlet APIs?
- At the beginning of JSP Servlet Development
- Java Servlet API documentation
- Integrate JSP and PHP in Apache
- Functions and principles of several encodings in JSP and Servlet