Java小程序静态类

发布于 2024-11-26 17:28:20 字数 368 浏览 1 评论 0原文

我的问题是将一些用户输入存储在小程序中以减少用户操作。 我为此使用静态字段。但我注意到,如果用户转到浏览器中的多个选项卡 - 新的“线程”(或类似的东西)启动,并且这个新线程中的静态类是空的。也许还有另一种解决方案可以在小程序中保存一些数据?

编辑

好的。更多细节。我有一个用于数字签名的小程序。用户一旦选择证书(X509Certificate)并使用它来签署所有文档。我有一堂这样的课:

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

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;
        ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-12-03 17:28:20

使用静态字段在应用程序的各个部分之间传递用户输入是一种非常糟糕的做法。数据应存储在根据需要传递的对象中。

但就您而言,为了在多个小程序之间共享数据,最好的解决方案是使用 java.net.CookieHandler 将其存储在浏览器 cookie 中

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

只涨不跌 2024-12-03 17:28:20

不同浏览器选项卡中的小程序是独立的程序。根据浏览器和 Java 插件,它们甚至可能在不同的 VM 中运行,但即使在同一 VM 中,它们也很可能具有独立的类加载器,并且无法通过静态变量进行通信。

如果需要存储用户数据,可以使用 JNLP API,例如 PersistenceService。

对于 1.6 Sun 插件,只有当您的小程序由 JNLP 加载时,此功能才可用,在 IcedTea 插件(与某些版本的 OpenJDK 一起分发)中,它也可用于由常用小程序标签加载的小程序(无 JNLP)。

(我不知道 Applet 如何使用 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.)

Spring初心 2024-12-03 17:28:20

听起来您不应该使用静态字段来存储用户数据。另外,我不认为新的浏览器选项卡会创建新线程,因为它应该是小程序的完全独立的实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文