Skip to content
vankeisb edited this page Jan 28, 2013 · 2 revisions

Woko includes some plumbing for handling browser caching via an URL cache token and HTTP response headers.

In order to enable static resource caching, you need to declare the filter in your web.xml :

<filter>
        <description>
            Http Caching of static resources
        </description>
        <display-name>WokoHttpCacheFilter</display-name>
        <filter-name>WokoHttpCacheFilter</filter-name>
        <filter-class>woko.util.httpcaching.WokoHttpCacheFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>WokoHttpCacheFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
</filter>

Then, in your JSPs you can use the <w:cacheToken/> JSP tag. It allows you to store the cacheToken request parameter name and token value in the page as variables. You can then use these variables in order to create links to static resources that will be cached by the browser forever.

Here's an example for including a myStyle.css file, in a JSP :

<w:cacheToken paramName="cacheToken" tokenValue="cacheTokenValue"/>
<c:set var="cacheTokenParams" value="${cacheToken}=${cacheTokenValue}"/>
...
<link rel="stylesheet" type="text/css" href="${cp}/myStyle.css?${cacheToken}=${cacheTokenValue}">

Changing the cacheToken value allows to force browser reload as the URL, which includes the cache token, has changed, and the browser considers the URL as pointing to a new resource.

The cacheToken is initialized with the request parameter name cacheToken and with a value of System.currentTimeMillis() at the moment the webapp started. You can override those values by configuring the filter (web.xml filter init parameters) or extending it.