Question

Currently I'm using a modified version of a Servlet Filter described here:

http://onjava.com/pub/a/onjava/2004/03/03/filters.html

And have hooked it up to the stripes dispatcher servlet like this:

 <filter-mapping>
        <filter-name>CacheControl</filter-name>
        <servlet-name>StripesDispatcher</servlet-name>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

Which works: but has the effect of controlling all my content - how could I get control on a per JSP basis and/or a per actionBean method basis ?

I was going to try and extend 'ForwardResolution' and inject here, but don't seem to be able to do this in my own package-structure ? (Protected constructor I think).

Two other ideas I had (which I don't particular like the sound of) are:

  1. Creating a random cache key on all JSPs which call into the ActionBean (thereby invalidating the cache control - in effect).
  2. Passing in a HTTP parameter, which I can then check for in the CacheControl Filter as to whether to add in the cache-control headers.
  3. Can I just add the directive to my JSPs ? What's the recommended way of doing this.

[as an aside, it appears that my filter is being invoked twice for every invocation...is this a side-effect of the way I have hooked up a filter to the stripes servlet maybe?]

Was it helpful?

Solution 2

Actually - further digging revealed that stripes 1.5 has a annotation-mechanism for controlling HTTP cache headers:

http://www.stripesframework.org/display/stripes/News

// Client cache control. The new @HttpCache annotation can be applied to an ActionBean class and/or event handler method to control how the client caches the response. Annotations on methods override those on classes and the annotation is inherited from superclasses. This is especially useful for AJAX calls from IE. //

OTHER TIPS

I'll start backwards - your filter is invoked twice, perhaps because the same page requests two resources - the page, and a css, for example. Or makes an ajax-call.

Then, if you want to control the cache per-requested resource, here are a two servlet-only solution. I don't know stripes, so there might be a better one there:

  • create an utility class+method, say CacheUtils.addCacheHeaders(response). In this method add all cache headers to the response. Invoke it anywhere you like - one line for adding caches. This is a programmatic approach

  • Define a (comma-separated) list of request urls in an init-param of the filter. The in the init(..) method parse the param, store it in a list, and apply cache only if the current request uri (request.getRequestURI()) matches one in the list. That's a declarative approach

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top