Filter in Servlet

Source: Internet
Author: User

 

Filter: filter technology is a new servlet 2.3 function)

Filter allows you to change a request or modify a response. A filter is not a servlet. It cannot generate a response, but it can process the request before a request arrives at the servlet. It can also process the response when a response leaves the servlet.

A filter includes:
1. intercepted before the servlet is called;
2. Check the Servlet request before the servlet is called;
3. Modify the request header and request data as needed;
4. Modify the response header and response data as needed;
5. After the servlet is called, it is intercepted.

The correspondence between filters and servlets is many-to-many. That is to say, you can configure one filter to one or more Servlets, and one servlet can have multiple filters. Some useful filters include: User Identification filter, LOG filter, audit filter, encryption filter, symbol filter, and XSLT filter that can change XML content.
A filter must implement the javax. servlet. Filter interface and define three methods:
1. Void setfilterconfig (filterconfig config) // sets the configuration object of the filter;
2. filterconfig getfilterconfig () // return the configuration object of the filter;
3. Void dofilter (servletrequest req, servletresponse res, filterchain chain) // executes the filter operation.

The server only calls the setfilterconfig method once to prepare the filter processing. The dofilter method is called multiple times to process different requests. you can find the Filter Name and initialization parameter information through the filterconfig interface. the server can set filterconfig to null to indicate that the filter has ended.
Each filter obtains the current request and response from the dofilter () method. in this method, you can perform any request and response operations. (including data collection and packaging ). filter calls chain. the dofilter () method gives control to the next filter. A filter ends in the dofilter () method. if a filter wants to stop request processing and get full control over response, it can not call the next filter.
A filter can wrap request or response to change several methods and provide custom attributes. api2.3 provides httpservletrequestwrapper and httpservletresponsewrapper. they can dispatch the initial request and response. to change the features of a method, you must inherit the wapper and override methods. below is a simple LOG filter used to record the duration of all requests.
Public class logfilter implements filter {<br/> filterconfig config; <br/> Public void setfilterconfig (filterconfig config) {<br/> This. config = config; <br/>}< br/> Public filterconfig getfilterconfig () {<br/> return config; <br/>}< br/> Public void dofilter (servletrequest req, <br/> servletresponse res, <br/> filterchain chain) {<br/> servletcontext context = getfilterconfig (). getservletcontext (); <br/> long BEF = system. currenttimemillis (); <br/> chain. dofilter (req, Res); // No chain parameter needed here <br/> long aft = system. currenttimemillis (); <br/> context. log ("request to" + req. getrequesturi () <br/> + ":" + (aft-BEF); <br/>}< br/>}
When the server calls setfilterconfig (), the filter saves the config information. use the config information in the dofilter () method to obtain the servletcontext. to run this filter, you must configure it to the Web. in XML. take tomcat4.01 as an example:
<Filter> <br/> <filter-Name> <br/> log // Filter Name <br/> </filter-Name> <br/> <filter-class> <br/> logfilter // filter class (servlet in the previous example) <br/> </filter-class> <br/> </filter> <br/> <filter-mapping> <br/> <filter-Name> log </Filter -Name> <br/> <servletname> servletname </servlet-Name> <br/> </filter-mapping> <br/> <servlet> <br/> <Servlet -Name> servletname </servletname> <br/> <servletclass> servletclass </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> servletname </servlet-Name> <br/> <URL-pattern> * </url-pattern> <br/> </servlet-Mapping> 


From the above example, we can see that the filter and Servlet are configured in Web. xml.

 

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.