2

What approaches are available for caching within a Java applet?

I gather that the .jar that makes up the applet will be cached by most browsers.

  • Will this be the case for any dependent .jars used by the applet?
  • If the applet loads resources from a remote URL at runtime, is it correct to assume that this will not be cached by the browser? If it is not cached by the browser, would one be able to implement caching by writing to local storage?
DeejUK
  • 12,891
  • 19
  • 89
  • 169

3 Answers3

3

Will this be the case for any dependent .jars used by the applet?

Yes, assuming that the dependent JARs are cacheable.

If the applet loads resources from a remote URL at runtime, is it correct to assume that this will not be cached by the browser?

Probably yes. The JVM will probably connect directly to the remote server, and the browser won't see the HTTP request. In addition, the JVM will probably be unaware of the browser's cache organization or location. However, this is all platform dependent.

It is also possible that the JVM could implement its own HTTP cache. AFAIK, current generation Oracle JVMs don't, but it is not inconceivable that future ones might.

If it is not cached by the browser, would one be able to implement caching by writing to local storage?

Only if the applet is signed, and the user has accepted the signature. An applet normally can't read or write local storage.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for the answer. Just to clarify the runtime caching behaviour: if my Java applet makes an HTTP request of its own, say to load an image, would that be treated like a resource the browser loads itself, or is the browser circumvented and the the JVM makes the request 'directly'? – DeejUK Apr 11 '12 at 10:59
  • If the JVM sandbox permits the applet to do that, then I think that the browser won't be aware that the request is made. I also think that the JVM won't be able to use the browser's cache directly ... because it won't know how it is organized, or even where it is. – Stephen C Apr 11 '12 at 11:02
  • Thanks for the clarification. You may want to amend your second point to "yes", and then I can mark your answer as correct. – DeejUK Apr 11 '12 at 14:04
  • @Deejay - I see what you mean. I misunderstood that part of your question. – Stephen C Apr 11 '12 at 14:35
  • Hi Stephen, I found another StackOverflow answer that suggests that using URLConnection will go through the browser's cache. See my answer below. – DeejUK Apr 13 '12 at 07:51
2

For better control of resource caching, deploy the applet using Java Web Start which offers:

..automatic update (including lazy downloads and programmatic control of updates)..

Note that a JWS app. does not need to be trusted to invoke the programmatic updates part of the JNLP API.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

According to How to disable http caching in applet and URLConnection JavaDoc caching will be enabled when requesting a resource programatically.

Community
  • 1
  • 1
DeejUK
  • 12,891
  • 19
  • 89
  • 169
  • Note that `setUseCaches` talks about **allowing** caches to be used. I think it is referring to caching in external HTTP proxies. But feel free to experiment see if this does make use of the browser's cache and/or a JVM specific cache. – Stephen C Apr 13 '12 at 09:27
  • Yep, I think some empirical tinkering is required :) – DeejUK Apr 13 '12 at 09:43