28

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?

4 Answers 4

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
    Commented Jul 14, 2009 at 1:17
  • 2
    LOL... where's the love?
    – KPWINC
    Commented Jul 14, 2009 at 21:48
  • 4
    Where do i change the chachingAllowed tag?
    – kajacx
    Commented Feb 13, 2014 at 12:28
  • 1
    Got a warning from Tomcat 8 when starting: Setting property cachingAllowed to false did not find a matching property. Commented Sep 28, 2015 at 10:49
  • 4
    This answer is now super out-of-date, for anyone reading, and is no longer really correct (for Tomcat 5+)
    – Alkanshel
    Commented Aug 23, 2016 at 23:46
18

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
    Commented Apr 1, 2016 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."
    – Alkanshel
    Commented Aug 23, 2016 at 23:24
  • @Amalgovinus for Tomcat 7 see Zalumon's answer. Commented Sep 22, 2016 at 15:05
  • @Amalgovinus did you resolve your issue? I have same problem in tomcat7, what did you do?
    – Aaron
    Commented Feb 27, 2017 at 11:29
  • I never got the property to work.. but I'm pretty sure my cache was an unrelated deployment issue :( user error
    – Alkanshel
    Commented Feb 27, 2017 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
    Commented Feb 27, 2017 at 11:35
  • 1
    As attributes of the context element. Commented Feb 28, 2017 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. Commented Jul 14, 2009 at 13:49
  • 1
    Then it's gotta be from browser cache
    – lisak
    Commented Apr 7, 2011 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .