OSCache comes with a servlet filter that enables you to transparently cache entire pages of your website, and even binary files. Caching of binary files is extremely useful when they are generated dynamically, eg PDF files or images.

To configure the filter, add something like the following to your web.xml file (obviously you will want to set the URL pattern to match only the content you want to cache; this example will cache all JSP pages):


   <filter>
      <filter-name>CacheFilter</filter-name>
      <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
   </filter>
   <filter-mapping>
      <filter-name>CacheFilter</filter-name>
      <url-pattern>*.jsp</url-pattern>
   </filter-mapping>

The default duration is one hour and the default scope for the cache is application scope. You can change these settings using initialization parameters. The time parameter sets the cache time (in seconds), while the scope parameter lets you set the scope. Valid values for the scope are application and session.

As an example, the following settings would cache content for 10 minutes in session scope:


   <filter>
      <filter-name>CacheFilter</filter-name>
      <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
      <init-param>
         <param-name>time</param-name>
         <param-value>600</param-value>
      </init-param>
      <init-param>
         <param-name>scope</param-name>
         <param-value>session</param-value>
      </init-param>
   </filter>

Note that the filter will only cache content that has a status of 200 (HttpServletResponse.SC_OK).