We use Tomcat and have written a simple filter to set the cache
control headers on static content. (see below)

We handle the issue with changing JS and CSS files by versioning them
on releases. So when we update a app to version 2 we rename the JS and
CSS files to add a version number so the new pages wont get the old JS
from a cache.

Here is the filter to set headers:
public class SetHeadersFilter implements Filter
{
    private FilterConfig _filterConfig;

    public void doFilter(ServletRequest req, ServletResponse res,
FilterChain filterChain)
        throws IOException, ServletException
    {
        HttpServletResponse response = (HttpServletResponse) res;

        for (Enumeration<?> e = _filterConfig.getInitParameterNames();
e.hasMoreElements();)
        {
            String header = (String) e.nextElement();
            response.setHeader(header, _filterConfig.getInitParameter(header));
        }

        filterChain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig)
    {
        this._filterConfig = filterConfig;
    }

    public void destroy()
    {
        this._filterConfig = null;
    }

}

Here is how we use the filter in web.xml:
  <filter>
    <description>Cache-Control for static resources</description>
    <filter-name>StaticCacheControlFilter</filter-name>
    <filter-class>com.allmanint.common.servlet.SetHeadersFilter</filter-class>
    <init-param>
      <param-name>Cache-Control</param-name>
      <param-value>max-age=3600, must-revalidate</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>StaticCacheControlFilter</filter-name>
    <url-pattern>/images/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>StaticCacheControlFilter</filter-name>
    <url-pattern>/css/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>StaticCacheControlFilter</filter-name>
    <url-pattern>/js/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>StaticCacheControlFilter</filter-name>
    <url-pattern>*.ico</url-pattern>
  </filter-mapping>



On Mon, Jun 14, 2010 at 9:14 AM, James Cook <james.c...@wecomm.com> wrote:
> Unfortunately they don't say how they managed it, it is mostly people
> complaining that when they change their JS file, without modification to
> the url that accesses the resource the user would have to ctrl+f5 it...
>
> Sorry :(
>
> -----Original Message-----
> From: RogerV [mailto:roger.var...@googlemail.com]
> Sent: 14 June 2010 12:57
> To: user@struts.apache.org
> Subject: RE: Struts 2 & Browser Caching
>
>
>
>
> James Cook-13 wrote:
>>
>> Nope, no misunderstanding. All I was saying was that people seem to
>> experience the opposite to what you are experiencing. Like you said,
>> they have what you want...
>>
>
> I don't suppose that you happen to have one of your google searches to
> hand
> do you? I'm obviously using the wrong keywords :(
>
> Regards
>
> --
> View this message in context:
> http://old.nabble.com/Struts-2---Browser-Caching-tp28876782p28878618.htm
> l
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to