问题
I am migrating Java 1.6 applets to Java 1.7. One of our applets periodically hits an URL to retrieve a dynamic status value:
https://host/myapp/path/to/status
And then it updates according to the latest status value. Since upgrading to Java 1.7, my client does not retrieve the latest status value. I see entries like this in the Java console:
CacheEntry[https://host/myapp/path/to/status]: updateAvailable=true,lastModified=Wed Dec 31 17:00:00 MST 1969,length=82
It looks like the client has some cached value for that URL and isn't actually retrieving the latest dynamic value from the server.
This worked fine as a 1.6 applet, and it also works fine as a 1.7 standalone Java application. How can I disable or bypass this caching behavior when running as a 1.7 applet?
回答1:
I've resolved this problem by using the following HTTP header in the server response:
Cache-Control: no-cache, no-store, no-transform
Now my Java 1.7 applet fetches the data from the server every time. Apparently Oracle changed how applets respond to these headers in Java 1.7. Previously (when we were using Java 1.6 applets), we had the following header:
Cache-Control: no-cache
and that worked in Java 1.6 but not in 1.7.
回答2:
Had a similar problem with a Java applet and this fixed it:
Go to Control Panel (or System Preferences) > Java > General > Settings and then uncheck the box that says "Keep temporary files on my computer".
回答3:
I'm having the same problem. I tried using URLConnection.setUsesCaches(false)
but that does absolutely nothing.
I had to add the following to an http config file for apache. Then it worked.
<Directory /foo>
<FilesMatch "\.(xml)$">
Header set Cache-Control "no-cache, no-store, no-transform"
</FilesMatch>
</Directory>
回答4:
I already solved this problem that has been happening to me as well.
I add some fake arguments at the end of the URL such as date time or what ever that change every time i call the url.
for my own solution:
I genereate a date string
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );
String today = formatter.format( new java.util.Date() );
and after that when i call the url i put the date sstring to the end of the url
new URL("http://url.to.something?fake_query_string=" + today);
with this solution, it'll fool java as a new url, so there's no any cache for this url, then java would always retrieve the data.
PS. it wouldn't be the best solution, but it saves me.
回答5:
For me using setUseCaches(false) alone did not work. But calling both the following methods did work:
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
I suspect the reason is that my original url redirects to another url. The above code should work for all cases.
来源:https://stackoverflow.com/questions/17181129/java-1-7-applet-cacheentry-preventing-dynamic-updates