Java: what is the use of servletconfig filterconfig?

Source: Internet
Author: User

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;
}
}

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.