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