Filter notes, filter

Source: Internet
Author: User

Filter notes, filter

Filter notes

 

I,Filter Basics:

 

1. The filter cannot create request and response, but can change request and response.

 

2. Filter execution sequence:

<1>. Run the code before the chain. doFilter (request, response) method;

<2>. Run the chain. doFilter (request, response) method. This method redirects to the next Filter. If no Filter is available, the method jumps to JSP/servlet;

<3> If the. JSP/servlet execution is complete, the code after the chain. doFilter (request, response) method is executed is returned.

 

3. Use:

<1>. Create a Filter package;

<2> Create a Filter class, implement the Filter interface (javax. servlet. Filter), and implement three implementation methods;

<3>. Configure the web. xml file

 

4. How to implement and execute multiple filters in sequence:

When the URLs of multiple filters are the same, the Filter chain is formed. The execution order is the top-down arrangement order in web. xml.

 

 

 

II,Filter advanced:

1. The configuration options in the <dispatch> label can determine the filtered request. The default value is request.

 

2. servlet2.5 (J2EE5): the Filter has four configuration options: request, forward, include, and error.

 

3. servlet3.0 (J2EE6): the Filter has five configuration options, and an ASYNC (asynchronous configuration option) is added. It is often used to: Release servlet to slow execution, without waiting for asynchronous simultaneous execution of chain. method After the doFilter method.

 

4. servlet3.0 has an additional Filter annotation mechanism, @ WebFilter. You do not need to configure it in web. xml.

 

5. the error method is often used to record the filtering information in the Filter. an error page is configured in xml. When an error occurs, the program jumps to the error page and triggers the error Filter to record the error information logs in the Filter method. Note: You must set <dispatcher> to error when using the service. Otherwise, the request will not capture the information by default.

 

 

III,Common situations:

<1>. Implement the log function;

<2>. User-Defined security functions;

<3>. debug the function;

<4>. encryption;

<5>. Data Compression;

<6>. Change the request or response;

<7>. Fixed the encoding problem.

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.