26

How do I prevent tomcat from caching? I have css and basic html files that I load and use via ajax, and unless I restart tomcat, the changes do not seem to reflect. Different machines, different browsers, and I don't receive the updated files.

Thoughts?

21

You might have to delete the application cache folder in /work/Catalina/localhost after changing the cachingAllowed flag.

Configuration can be introduced in server.xml as

<Context className="org.apache.catalina.core.StandardContext"
                 cachingAllowed="false"
                 charsetMapperClass="org.apache.catalina.util.CharsetMapper"
                 cookies="true" 
                 reloadable="false" 
                 wrapperClass="org.apache.catalina.core.StandardWrapper">
        </Context>
6
  • 2
    +1 I completely forgot to mention that in my original answer. Thanks! – KPWINC Jul 14 '09 at 1:17
  • 2
    LOL... where's the love? – KPWINC Jul 14 '09 at 21:48
  • 4
    Where do i change the chachingAllowed tag? – kajacx Feb 13 '14 at 12:28
  • 1
    Got a warning from Tomcat 8 when starting: Setting property cachingAllowed to false did not find a matching property. – naXa Sep 28 '15 at 10:49
  • 4
    This answer is now super out-of-date, for anyone reading, and is no longer really correct (for Tomcat 5+) – Amalgovinus Aug 23 '16 at 23:46
15

For Tomcat 8 / Tomcat 9 properties should be added in conf/context.xml as follows

<Context>
  <Resources antiResourceLocking="false" cachingAllowed="false" />
  ...
</Context>

You might have to delete the application cache folder in /work/Catalina/localhost after changing the cachingAllowed flag. Also clear the cache of IntelliJ IDEA (if you use it to run Tomcat):

Mac:     /Users/{:user}/Library/Caches/IntelliJIdea{:version}/tomcat/  
Linux:   /home/{:user}/.IntelliJIdea{:version}/system/tomcat/
Windows: C:\Users\{:user}\.IntelliJIdea{:version}\system\tomcat\

See Apache Tomcat 9 Configuration Reference for other parameters.

5
  • 2
    antiResourceLocking on <Resources>-Tag is definitively wrong for tomcat 8 – pscheit Apr 1 '16 at 12:13
  • This isn't correct for tomcat 7, either. "Setting property 'antiResourceLocking' to 'false' did not find a matching property." ... "Setting property 'cachingAllowed' to 'false' did not find a matching property." – Amalgovinus Aug 23 '16 at 23:24
  • @Amalgovinus for Tomcat 7 see Zalumon's answer. – naXa Sep 22 '16 at 15:05
  • @Amalgovinus did you resolve your issue? I have same problem in tomcat7, what did you do? – Aaron Feb 27 '17 at 11:29
  • I never got the property to work.. but I'm pretty sure my cache was an unrelated deployment issue :( user error – Amalgovinus Feb 27 '17 at 17:39
13

I had this problem in Tomcat 7 and the reason was that I had antiResourceLocking set to true (it sounded like a good idea...).

According to the docs ( http://tomcat.apache.org/tomcat-7.0-doc/config/context.html ):

Please note that setting this to true has some side effects, including the disabling of JSP reloading in a running server: see Bugzilla 37668.

In my case it even caused plain static text files to be cached.

So, in summary, at least for rapid development I had to use:

antiResourceLocking="false"
cachingAllowed="false"
2
  • Where do I add this exactly? If I put in conf/context.xml it gives me "did not find a matching property" for each of them... – Aaron Feb 27 '17 at 11:35
  • 1
    As attributes of the context element. – Reto Höhener Feb 28 '17 at 17:09
4

Have you checked this documentation: Apache Tomcat Configuration Reference ?

cacheMaxSize -- Maximum size of the static resource cache in kilobytes. If not specified, the default value is 10240 (10 megabytes).

cacheTTL -- Amount of time in milliseconds between cache entries revalidation. If not specified, the default value is 5000 (5 seconds).

cachingAllowed -- If the value of this flag is true, the cache for static resources will be used. If not specified, the default value of the flag is true.

These parameters are the same for Tomcat 5.5 and Tomcat 6.0.

2
  • I set cachingAllowed to false and cacheTTL to 1000 in my appcontext.xml, and I STILL can't get file not to cache. Worse yet, it's caching across tomcat restarts. – Stefan Kendall Jul 14 '09 at 13:49
  • Then it's gotta be from browser cache – lisak Apr 7 '11 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.