File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Static content caching problem in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Static content caching problem in Struts2" Watch "Static content caching problem in Struts2" New topic

Static content caching problem in Struts2

Prakash Saptagiri

Joined: Feb 16, 2010
Posts: 4
I have been trying to add caching headers to my webapp's static content with no luck, I am trying to set a far future date for all my static contents in my webapp.

I created a Filter servlet to add a desired HTTP header for all the static response with file extensions provided in the web.xml , while debugging I noticed that all the headers did get set with the appropriate HTTP headers inside my Filter servlet class but were getting overridden by the Struts2 FilterDispacher to be the default period of caching provided by the Struts2 framework which only caches static contents for a day.

While going throw the Struts2 source for , I can see the following code flow inside the doFilter() method of

serveStatic in the above code is a StrutsConstant "struts.serve.static=true" in , I tried making it false in my file hoping to make struts2 not to overwrite my static response headers (the ones I set in my personal Filter class), that didn't go well , all the JSP pages were served without static contents .

I am not using StrutsConstant - serveStaticBrowserCache("struts.serve.static.browserCache") as it puts the same 1 day expires header if 'True' or puts 'no-cache' is false.

My static contents are in resource path '/struts' eg. /struts/imgs/fav.png (or) /struts/css/mainstyle.css etc. for which I am trying to set a far future expires date.

I want to know how to tell struts not to put default headers for static response and if that not possible ,
How to make struts accept a external expires duration(eg. 90 days) as by default assumes 1 days as the duration for caching static contents.

Calling my Filter after FilterDispacher in web.xml does not work either , I am not sure if that's possible

Please advice how I can manipulate my response headers for my static contents in my webapp to set a far future date,

Any help/response to solve this problem is appreciated ,

Ankit Garg

Joined: Aug 03, 2008
Posts: 9462

Howdy Prakash, welcome to javaranch.

Prakash please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prakash Saptagiri

Joined: Feb 16, 2010
Posts: 4
I have found a solution that works for me to solve the caching issue , I dont know if this is the only way but it sure does work for me,

I used @Override annotation to override the default struts FilterDispacher's findStaticResource method , this is the method which was putting 1 day of caching for all the static resources passing through struts.

I override this method to replace the logic where it puts 1 day of caching for static content and replaced it with my desired days of caching.


In the original findStaticResource method it was like the following , which was caching static contents for 1 day , above override fixed this problem,

// set heading information for caching static content
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
response.setHeader("Date",df.format(cal.getTime())+" GMT");
response.setHeader("Expires",df.format(cal.getTime())+" GMT");
response.setHeader("Retry-After",df.format(cal.getTime())+" GMT");
response.setHeader("Last-Modified",lastModified+" GMT");


I agree. Here's the link:
subject: Static content caching problem in Struts2
jQuery in Action, 3rd edition