Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

serialization - Object persistence in Java applets

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...