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.
For a trusted applet, there are many options.
Put the information in a sub-directory of
user.home.user.homewill be a place that is writable.I’ve heard that the
Preferencesclass can be used for that ..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 aPropertiesfile (for simple name/value pairs) ofXMLEncoder/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 thePersistenceService.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.