Filter is responsible for the creation and destruction of the Web server. When the Web application starts, the Web server creates an instance object of filter, invokes its Init method, completes the initialization of the object, and prepares the interceptor for subsequent user requests, and the filter object is created only once, and the Init method executes only once. The Filterconfig object representing the current filter configuration information can be obtained through the parameters of the Init method.
Web The container calls the Destroy method to destroy the filter. The Destroy method executes only once in the life cycle of the filter. In the Destroy method, you can release the resources used by the filter.
filters allow for flexible configuration to handle complex environments. Allows the filter to be configured flexibly is the Filterconfig interface.
1. Filterconfig Interface
When you configure the filter , you can use <init-param> to configure some initialization parameters for the filter, when the Web container instantiates the filter object and calls its Init method. The Filterconfig object that encapsulates the filter initialization parameter is passed in. As a result, when writing the filter, the developer can obtain: by Filterconfig the object's method.
String getfiltername (): Gets the name of the filter.
string Getinitparameter (string name): Returns the value of the initialization parameter for the name specified in the deployment description. Returns null if it does not exist.
enumeration Getinitparameternames (): Returns an enumeration collection of the names of all initialization parameters for the filter.
Public Servletcontextgetservletcontext (): Returns a reference to the Servlet context object.
2. JAVA Implementation of the filter configuration information by using Filterconfig
Java code for the filter :
Package com.filter;
Import java.io.IOException;
Import java.util.Enumeration;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
/**
* a simple filter with filter configuration
* @author Fan Fangming
*/
public class Easyfilterconfig Implementsfilter {
@Override
Publicvoid Init (Filterconfig filterconfig) throws Servletexception {
System.out.println ("---- Filter Initialization----");
// get the name of the filter
Stringfiltername = Filterconfig.getfiltername ();
// get The initialization parameters configured in the Web. xml file
STRINGINITPARAM1 = Filterconfig.getinitparameter ("name");
STRINGINITPARAM2 = Filterconfig.getinitparameter ("like");
// returns an enumeration collection of the names of all initialization parameters for the filter.
Enumeration<string>initparameternames = Filterconfig
. Getinitparameternames ();
System.out.println (" Filter Name:" + filtername);
System.out.println (" initial parameter name:" + initParam1);
System.out.println (" initial parameterlike:" + initParam2);
while (Initparameternames.hasmoreelements ()) {
Stringparamname = (String) initparameternames.nextelement ();
System.out.println (" initial parameters:" + paramname);
}
}
// filter functions are implemented here
@Override
Publicvoid DoFilter (servletrequest request, servletresponse response,
Filterchainchain) throws IOException, Servletexception {
// Some preprocessing of request and response
Request.setcharacterencoding ("UTF-8");
Response.setcharacterencoding ("UTF-8");
Response.setcontenttype ("Text/html;charset=utf-8");
System.out.println ("# # # filter before execution!!! ");
Chain.dofilter (request,response);// let the target resource execute, release
System.out.println ("--- filter after execution!!!" ");
}
@Override
Publicvoid Destroy () {
System.out.println ("---- Filter Destruction----");
}
}
3. Add a filter to the Web. XML
<!-- Configure Filters -
<filter>
<filter-name>easyFilter</filter-name>
<filter-class>com.filter.EasyFilterConfig</filter-class>
<!-- Configure The initialization parameters of the Easyfilterconfig filter--
<init-param>
<description> Configure initialization parameters for the Easyfilterconfig filter </description>
<param-name>name</param-name>
<param-value>ffm</param-value>
</init-param>
<init-param>
<description> Configure initialization parameters for the Easyfilterconfig filter </description>
<param-name>2</param-name>
<param-value>java2</param-value>
</init-param>
<init-param>
<description> Configure initialization parameters for the Easyfilterconfig filter </description>
<param-name>3</param-name>
<param-value>java3</param-value>
</init-param>
</filter>
<!-- Map Filters --
<filter-mapping>
<filter-name>easyFilter</filter-name>
<!-- "/*" means to intercept all requests--
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Parameter description
<description> used to add descriptive information, the content of the element can be empty,<description> can not be configured.
<filter-name> used to specify a name for the filter, and the content of the element cannot be empty.
<filter-class> element is used to specify the full qualified class name of the filter.
<init-param> The element is used to specify the initialization parameters for the filter, its child elements <param-name> The name of the specified parameter,<param-value> the value of the specified parameter. In a filter, you can use the Filterconfig interface object to access the initialization parameters. If the filter does not need to specify initialization parameters, then the <init-param> element may not be configured.
4. Test Results
When the Web service is started, it is automatically loaded and executed.
---- Filter Initialization----
Filter Name: Easyfilter
Initial parameter name: FFM
initial parameterlike: null
Initial parameters: 3
Initial parameters: 2
Initial parameters: Name
A good memory is better than a bad pen. Filter filters in 30-java applications (2)