|
You can search for "filter" on Google. filterconfig can obtain the filter initialization parameters allocated in the deployment descriptor file (Web. XML.
In response to your question, the result is that filterconfig can obtain the Web. xml file and use filter as the parameter to describe in the tag.
Definition:
The filterconfig object provides access to the servlet environment and the filter name specified in the web. xml file.
The filterconfig object has a getinitparameter method that can access the filter initialization parameters allocated in the deployment descriptor file (Web. XML.
Instance:
Add the following code to Web. xml. You can use filterconfig to obtain the parameter that describes the tag using filter.
<! -- The cache filter -->
<Filter>
<! -- Design the filter processing class to generate static pages -->
<Filter-Name> cachefilter </filter-Name>
<Filter-class> com. jspbook. cachefilter </filter-class>
<! -- URLs that do not need to be cached -->
<Init-param>
<Param-Name>/timemonger. jsp </param-Name>
<Param-value> nocache </param-value>
</Init-param>
<Init-param>
<Param-Name>/testcache. jsp </param-Name>
<Param-value> nocache </param-value>
</Init-param>
<! -- Cache timeout, in seconds -->
<Init-param>
<Param-Name> cachetimeout </param-Name>
<Param-value> 600 </param-value>
</Init-param>
<! -- Whether the cache is based on the browser's different region settings (the generated cache file is in the format of test. jspid = zh_cn) -->
<Init-param>
<Param-Name> locale-sensitive </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> cachefilter </filter-Name>
<URL-pattern> *. jsp </url-pattern>
</Filter-mapping>
Usage:
Filterconfig. getinitparameter ("locale-sensitive ");
Filterconfig. getinitparameter ("cachetimeout"); the result is 600.
Filterconfig. getinitparameter (request. getrequesturi (); The param-value corresponding to param-name is obtained.
Filter processing class:
Public class cachefilter implements filter {
Servletcontext SC;
Filterconfig FC;
Long cachetimeout = long. max_value;
Public void dofilter (servletrequest req, servletresponse res,
Filterchain chain) throws ioexception, servletexception {
Httpservletrequest request = (httpservletrequest) req;
Httpservletresponse response = (httpservletresponse) RES;
// Check if was a resource that shouldn't be cached.
String r = SC. getrealpath ("");
String Path = FC. getinitparameter (request. getrequesturi ());
If (path! = NULL & path. Equals ("nocache ")){
Chain. dofilter (request, response );
Return;
}
Path = R + path;
}
Public void Init (filterconfig ){
This. FC = filterconfig;
String Ct = FC. getinitparameter ("cachetimeout ");
If (CT! = NULL ){
Cachetimeout = 60*1000 * long. parselong (CT );
}
This. SC = filterconfig. getservletcontext ();
}
Public void destroy (){
This. SC = NULL;
This. FC = NULL;
}
} |