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:
- Authentication Filters
- Logging and Auditing Filters
- Image Conversion Filters
- Data Compression Filters
- Encryption Filters
- Tokenizing Filters
- Filters that trigger resource access events
- xsl/t Filters
- 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
- Throws a servletexception
- 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:
- Examine the request
- Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
- 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
- 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