Java applet 中的对象持久性

发布于 2024-12-13 01:51:33 字数 151 浏览 1 评论 0原文

保存 Java 小程序状态的好方法是什么?

我可以处理文件的对象序列化/反序列化,但不知道它应该放在哪里,或者是否有一些“注册表”,我可以在其中保存几个用户的设置。

这些设置取决于硬件,因此我想将其保存在客户端上。

已向小程序授予完全权限。

What is a good approach to save state of a Java applet?

I can deal with object serialization/deserialization to/from a file but don't know where it should be placed or if there's some 'registry' where I can just save a couple of user's settings.

Those settings are hardware dependent so I want to save it on client.

Full permission is given to the applet.

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

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

发布评论

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

评论(2

苏辞 2024-12-20 01:51:34

保存 Java 小程序状态的好方法是什么?

对于一个值得信赖的小程序,有很多选择。

我可以处理文件的对象序列化/反序列化,但不知道应该将其放置在哪里..

将信息放在 user.home 的子目录中。

  • user.home 将是一个可写的地方。
  • 子目录(例如基于小程序类的包名称)以避免与其他应用程序的设置文件冲突。

..或者如果有一些“注册表”,我可以在其中保存几个用户的设置。

我听说 Preferences 类可以用于此目的..

此数据持久存储在依赖于实现的后备存储中。典型的实现包括平面文件、特定于操作系统的注册表、目录服务器和 SQL 数据库。此类的用户无需关心后备存储的详细信息。

听起来很不错,不是吗?唯一的麻烦是我从来无法制作一个例子,其中值在运行之间保持不变!


对象序列化伴随着一个巨大的警告,即它可能随时中断。


我会选择您自己规范的文件位置(例如在 user.home 中)并使用 Properties 文件(对于简单的名称/值对)XMLEncoder/< a href="http://download.oracle.com/javase/7/docs/api/java/beans/XMLDecoder.html" rel="nofollow noreferrer">XMLDecoder (对于更复杂的 Java 豆子)。

对于前者,请参阅此 小例子。后者在 JavaDocs 顶部的一个简短示例中进行了描述。


当然,如果这个小程序部署在 Plug-In 2 架构的 JRE 中并且可以访问 JNLP API,那么它可以使用 PersistenceService。这是一个 演示。 PersistenceService 的。

即使是沙盒小程序也可以使用PersistenceService - 它类似于 Cookie 的概念,因为它适用于少量数据。

What is a good approach to save state of a Java applet?

For a trusted applet, there are many options.

I can deal with object serialization/deserialization to/from a file but don't know where it should be placed..

Put the information in a sub-directory of user.home.

  • user.home will be a place that is writable.
  • A sub-directory (e.g. based on the package name of the applet class) in order to avoid colliding with the settings files of other apps.

..or if there's some 'registry' where I can just save a couple of user's settings.

I've heard that the Preferences class can be used for that ..

This data is stored persistently in an implementation-dependent backing store. Typical implementations include flat files, OS-specific registries, directory servers and SQL databases. The user of this class needn't be concerned with details of the backing store.

Sounds neat, doesn't it? The only trouble is that I've never been able to make an example where the values persist between runs!


Object serialization comes with a huge warning that it might break at any time.


I'd go for a file location of your own specification (e.g. in user.home) and either use a Properties file (for simple name/value pairs) of XMLEncoder/XMLDecoder (for more complex Java beans).

For the former, see this little example. The latter is described in a short example at the top of the JavaDocs.


Of course, if this applet is deployed in a Plug-In 2 architecture JRE and has access to the JNLP API, it can use the PersistenceService. Here is a demo. of the PersistenceService.

Even a sand-boxed applet can use the PersistenceService - it is similar to the concept of Cookies in that it is intended for small amounts of data.

沧笙踏歌 2024-12-20 01:51:34

当需要在浏览器会话之间保留数据时,Applet Persistence API 似乎是一个好方法:http://docs.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/persistence.html

The Applet Persistence API seems to be a good approach when data needs to be persisted between browser sessions: http://docs.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/persistence.html

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