Cache Filter ver. 2.0


This is a Java servlet filter (as per Servlet API 2.3). CacheFilter lets you use cached output from your jsp pages. You will provide an initial parameter to this filter as an XML file where you will describe your cache policy: directory for saving cached data, names for pages you need to be cached, timeouts for keeping data in cache. So instead of calculation filter will output cached data directly.

How to use it:

a) download cacheflt.jar and save it in WEB-INF/lib

b) describe this filter in web.xml. The name for initial parameter is config. Parameter value is a path to XML file where you will describe your cache policy.
 


<filter>
  <filter-name>cacheFilter</filter-name>
  <filter-class>com.cj.cacheflt.cacheFilter</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>path_to_your_file</param-value>
  </init-param>
</filter>

cacheServlet provides a console:
 


<servlet>
  <servlet-name>cacheServlet</servlet-name>
  <servlet-class>com.cj.cacheflt.cacheServlet</servlet-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>path_to_your_file</param-value>
  </init-param>
</servlet>

c) describe a mapping for this filter in web.xml
 


<filter-mapping>
  <filter-name>cacheFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
</filter-mapping>

in this case filter will be on for the each .jsp file

Also you may exclude some of your files from the processing. Parameter exclude contains a regular expresion to match excluded urls. E.g.:
 


<filter>
  <filter-name>cacheServlet</filter-name>
  <filter-class>com.cj.cacheflt.cacheServlet</filter-class>
  <init-param>
    <param-name>config</param-name>
    <param-value>path_to_your_file</param-value>
  </init-param>
  <init-param>
    <param-name>exclude</param-name>
    <param-value>/login.jsp</param-value>
  </init-param>
</filter>

And now when you invoke any .jsp page: http://your_host/your_page.jsp cacheFilter will check out cache settings for the given page and proceed request. You can see live stats for cache through cacheServlet: http://host/servlet/cacheServlet

Configuration file is XML file. You must define the following elements:
  - cache directory: any existing directory on your server for cached data.
  - descriptions for cached pages

Config => (cache, +page)

cache - defines a directory for cache. It is just any existing directory on your server. Note: all files from this directory will be deleted!. E.g.:

<cache>c:/data/tmp</cache>

page - defines a cached page. Attributes are:
uri - URI for this page, time - cache policy. Possible values are: -1 - static data, will be cached after the first usage, any positive integer value - defines caching time in seconds,
type - content type for this page.
varyByParam attribute provides a semicolon-separated list of strings used to vary the output cache. By default, these strings correspond to a query string value sent with GET method attributes, or a parameter sent using the POST method. It is similar to an appropriate parameter in ASP.NET OutputCache directive. When this attribute is set to multiple parameters, the output cache contains a different version of the requested body for each specified parameter.
varyByHeader attribute does the same with HTTP headers.
nocache - describes a name for the session attribute disables cache for the request. If this attribute is presented in the session than the particularly request will be processed transparently. E.g.:
 


<page uri="p1.jsp" time="-1" type="text/html" varyByHeader="user-agent"/>
<page uri="dir/subdir/page.jsp" time="600" type="text/html" />

Here for example the first directive allows you to provide cached versions of p1.jsp for the different browsers.

Notes:

1. Servlet uses JAXP for parsing XML file.

2. Config file can be saved anywhere on your server. You may use the full path for setting data file location.

   For downloading:
    Filter:  cacheflt.jar
    Sample of config file:  cacheflt.xml
 

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.


     

Also in JSOS: