Introduction:
Similar to servlet, filter is a Java component. Before a request is sent to servlet, you can use the filter to intercept and process the request. After the servlet ends, you can also use the filter to process the response before sending the response back. The Web Container can use the web. xml deployment file to declare when to call the filter.
Note:
The filter also has a lifecycle: Init ()-> dofilter ()-> destroy ().
Example:
1. Java class
Public class encodingfilter extends httpservlet implements filter {
Private filterconfig;
// Initial Activation
Public void Init (filterconfig) throws servletexception {
This. filterconfig = filterconfig;
}
// Execute
Public void dofilter (servletrequest request, servletresponse response,
Filterchain ){
// The filtering method is written here.
}
// Destroy
Public void destroy (){
}
}
2. Web. xml configuration
<Filter>
<Filter-Name> encodingfilter </filter-Name>
<Filter-class> com. tongtech. bjvsp. sysmng. Filter. encodingfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> encodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
As shown above, this is a simple filter.
References: http://www.javaeye.com/topic/82530