Java Web Learning notes-servlet Filter

Source: Internet
Author: User
Http://blog.csdn.net/pengpenglin/

1. servlet filter concept:
**************************************** **************************************** *******
The servlet filter is defined in java servlet specification 2.3. It can check and modify the request and response objects of the servlet container.

The servlet filter does not generate request and response objects. It can only provide 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:
**************************************** **************************************** *******
A. the servlet filter can check and modify the servletrequest and servletresponse objects.
B. 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.
C. servlet filters can be connected together to form a pipeline effect and collaboratively Modify request and response objects.
**************************************** **************************************** *******

Iii. Functions of servlet filters:
**************************************** **************************************** *******
A. query the request and take corresponding actions.
B. Block the request-response pair so that it cannot be further transmitted.
C. Modify the request header and data. You can provide custom requests.
D. Modify the response header and data. Users can achieve this by providing custom response versions.
E. interact with external resources.
**************************************** **************************************** *******

Iv. application scenarios of servlet filters:
**************************************** **************************************** *******
A. Authentication Filtering
B. login and Audit filtering
C. Image conversion Filtering
D. Data Compression and filtering
E. Encrypted Filtering
F. Token Filtering
G. Resource Access trigger Event Filtering
H. XSL/T Filtering
I. 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:

A. 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.

B. 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.

B. 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:
**************************************** **************************************** *******
A. Implement the javax. servlet. Filter Interface
B. Implement the init method and read the initialization function of the filter.
C. Implement the dofilter method to respond to requests or filters
D. Call the dofilter method of the filterchain interface object to pass requests or responses to subsequent filters.
E. Destroy the filter.
**************************************** **************************************** *******

7. servlet filter:
**************************************** **************************************** *******
A. Create a filter instance for the servlet container
B. The filter instance calls the init method to read the initialization parameters of the filter.
C. The filter instance calls the dofilter method and determines whether the request is valid based on the value of the initialization parameter.
D. If the request is not properly blocked
E. If the request is valid, call the chain. dofilter method to send the request to the future
**************************************** **************************************** *******

8. servlet filter:
**************************************** **************************************** *******
A. The filter intercepts client requests.
B. reencapsulate servletresponse to provide custom output streams in the encapsulated servletresponse.
C. send requests to the future
D. Respond to Web Components
E. Obtain the custom output stream from the encapsulated servletresponse.
F. Write the response content to the buffer stream through the custom output stream.
G. Modify the response content in the buffer stream, clear the buffer stream, and output the response content.
**************************************** **************************************** *******

9. servlet filter release:
**************************************** **************************************** *******
A. When publishing a servlet filter, you must add the <filter> element and <filter-mapping> element to the Web. xml file.

B. 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

C. 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
**************************************** **************************************** *******
A. because filter, filterconfig, and filterchain are both 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.

B. Configure Servlet and Servlet filters in Web. xml. The filter element should be declared first, and then the servlet element should be declared.

C. If you want 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.w.suffix?#.txt"
Timestamp = "true"/>
**************************************** **************************************** *******

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.