How to configure cache-control in Tomcat

I ran into a major problem with IE (*shudder*) not being able to open files. I could save them fine, but when I went open, the application would complain that it couldn’t find the file. I found that the problem was not the java application but the webserver itself. Using a very useful tool called Fiddler I looked at the HTTP request headers and found that the webserver was overwriting the headers I set in java. I found that the headers were:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Mon, 09 Jul 2007 22:50:18 GMT
Content-type: application/octet-stream
X-powered-by: Servlet/2.4
Pragma: No-cache
Cache-control: no-cache
Expires: Thu, 01 Jan 1970 12:00:00 NZST
Content-disposition: attachment; filename=trace-64.pcap
Transfer-encoding: chunked

Setting no-cache means that IE downloads the file, but then it expires it in cache, it gets deleted and hence the application cannot open the file. After much searching around the internet, I found this post which describes how to do it.

I added the below to my META-INF/context.xml:

<Valve className="org.apache.catalina.authenticator.BasicAuthenticator"  disableProxyCaching="false" />

Once Tomcat is restarted, the headers are now:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=trace-4.pcap
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Date: Mon, 09 Jul 2007 22:59:01 GMT

IE now can open files!!!!!!!!! Credit goes to above post 😉
This works for me when included in the .war file, and it now works in Firefox and IE.

14 comments on “How to configure cache-control in Tomcat

  1. rtsan says:

    This solution works!

    Thank you,
    rtsan

  2. David says:

    This fixed a problem I had downloading swf files in IE8 from tomcat. Very easy fix. Thanks a ton!

  3. David says:

    I had SSL/IE/SWF loading problems! Now fixed!
    Greeaaat!

    Thanks a lot!

  4. Tom Cat says:

    This fix worked, thank you!!!

  5. Lou says:

    This fixed an issue we were having with IE8/SSL and PDF/XLS files. Thanks.

  6. watfor says:

    Thanks, this resolved an issue with IE8/CSV/SSL

  7. Sean says:

    Great! Thanks for your posting. I resolve it!!

  8. OKI says:

    yes …this is saving my weekend !!

  9. Rohan says:

    Thats Great But how do i do the reverse, explictly set an Expires header on some servlet responses??

  10. Gaurav says:

    I updated the context.xml located in Tomcat\webapps\manager\META-INF
    But still unable to open/save the documents.
    Could someone please help

    • Gaurav says:

      I added the below parameter in the tomcat/conf context.xml file and the fix worked. Now able to download attachments

  11. Kasun says:

    This fix worked, thank you!!!

  12. kanwalpreet singh says:

    Thanks …worked for my application.

Leave a reply to Rohan Cancel reply