Interceptors and filters

Source: Internet
Author: User

A filter is an object of performs filtering tasks on either the request to a resource (a servlet or static content), or On the response from a resource, or both.

Filters perform filtering in the doFilter method. Every Filter have access to a Filterconfig object from which it can obtain its initialization parameters, and a reference T o the servletcontext which it can use, for example, to load resources needed for filtering tasks.

Filters is configured in the deployment descriptor of a Web application.

Examples that has been identified for this design is:

    1. Authentication Filters
    2. Logging and Auditing Filters
    3. Image Conversion Filters
    4. Data Compression Filters
    5. Encryption Filters
    6. Tokenizing Filters
    7. Filters that trigger resource access events
    8. xsl/t Filters
    9. Mime-type chain Filter

The Init method called by the Web container to indicate to a filter that it's being placed into service.

The servlet container calls the Init method exactly once after instantiating the filter. The Init method must complete successfully before, the filter is asked to does any filtering work.

The Web container cannot place the filter into service if the Init method either

    1. Throws a servletexception
    2. Does not return within a time period defined by the Web container

The DoFilter method of the Filter is called by the container each time a request/response pair is passed through T He chain due to a client request for a resource at the end of the chain. The filterchain passed in to this method allows the Filter to pass on the request and response to the next entity in the C Hain.

A Typical implementation of this method would follow the following pattern:

    1. Examine the request
    2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
    3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
      • either invoke the next entity in the chain using the Filterchain object (chain.dofilter ()),
      • or not pass on the request/response pair to the next entity in the filter chain to block the request processing
    4. Directly set headers on the response after invocation of the next entity in the filter chain.

The Destroy method called by the Web container to indicate to a filter that it's being taken out of service.

This method was only called once all threads within the filter ' s DoFilter method had exited or after a timeout period had Passed. After the Web container calls this method, it won't call the DoFilter method again on this instance of the filter.

This method gives the filter a opportunity to clean up any resources that is being held (for example, memory, file Handl ES, threads) and make sure this any persistent state was synchronized with the filter's current state in memory.

Example:

Xml

1  <Filter>2     <Filter-name>Filter1</Filter-name>3     <Filter-class>Com.filter.FilterOne</Filter-class>4  </Filter>5  <filter-mapping>6     <Filter-name>Filter1</Filter-name>7     <Url-pattern>/*</Url-pattern>8  </filter-mapping>9     Ten  <Filter> One     <Filter-name>Filter2</Filter-name> A     <Filter-class>Com.filter.FilterTwo</Filter-class> -  </Filter> -  <filter-mapping> the     <Filter-name>Filter2</Filter-name> -     <Url-pattern>/*</Url-pattern> -  </filter-mapping>    
Filterone.java
1  Public classFilteroneImplementsFilter {2 3     //execution of the Filter chain4      Public voidDoFilter (ServletRequest arg0, Servletresponse arg1, Filterchain arg2)5             throwsIOException, servletexception {6         //Filtering the request7System.out.println ("Filter One Before");8 9         //Next Filter orTen Arg2.dofilter (arg0, arg1); One  A         //filtering on the response -System.out.println ("Filter One after"); -     } the  -     //Initialize Filter -      Public voidInit (Filterconfig arg0)throwsservletexception { -System.out.println ("Filter One init"); +     } -  +     //releasing the resources used by the filter A      Public voiddestroy () { atSystem.out.println ("Filter One Destroy"); -     } -  -}

Output:

Filter one before

Filter before

Filter both after

Filter one After

Interceptors and filters

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.