开发者

Java applet static class

my problem is store some user inputs in applet to reduce user operations. I use static fields for that. But I noticed, that if user goes to several tabs in browser - thr new "thread" (or something like this) started, and my static class in this new thread is empty. Maybe 开发者_开发技巧there is another solution to save some data in applet?

Edit

Ok. More details. I have an applet for digital signature. User once select certificate (X509Certificate) and use it for sign all documents. I have a class like this:

   public class CertificateContainer
    {
        private static X509Certificate certificate;
        ...


Using static fields to pass user input between parts of an application is a very bad practice. The data should be stored in objects that are passed around as necessary.

But in your case, for sharing data between multiple applets, the best solution would be to store it in Browser cookies using java.net.CookieHandler


Applets in different browser tabs are independent programs. Depending on the browser and Java-Plugin they may even run in a different VM, but even if in the same VM, they most probably have independent class loaders and will not be able to communicate by static variables.

If you need to store user data, you could use the JNLP API, for example the PersistenceService.

With the 1.6 Sun Plugin, this will only be available if your applet was loaded by JNLP, in the IcedTea Plugin (distributed with some versions of OpenJDK) it is also available for applets loaded by the usual applet tag (without JNLP).

(I have no idea how a CookieHandler would be used by Applets.)


It sounds like you shouldn't be using static fields for storing user data. Also, I don't think a new browser tab would create a new thread because it should be a completely separate instance of your applet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜