Java小程序静态类
我的问题是将一些用户输入存储在小程序中以减少用户操作。 我为此使用静态字段。但我注意到,如果用户转到浏览器中的多个选项卡 - 新的“线程”(或类似的东西)启动,并且这个新线程中的静态类是空的。也许还有另一种解决方案可以在小程序中保存一些数据?
编辑
好的。更多细节。我有一个用于数字签名的小程序。用户一旦选择证书(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用静态字段在应用程序的各个部分之间传递用户输入是一种非常糟糕的做法。数据应存储在根据需要传递的对象中。
但就您而言,为了在多个小程序之间共享数据,最好的解决方案是使用 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
不同浏览器选项卡中的小程序是独立的程序。根据浏览器和 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.)
听起来您不应该使用静态字段来存储用户数据。另外,我不认为新的浏览器选项卡会创建新线程,因为它应该是小程序的完全独立的实例。
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.