Object persistence in Java applets

前端 未结 2 1409
予麋鹿
予麋鹿 2020-12-22 05:39

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

相关标签:
2条回答
  • 2020-12-22 06:22

    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.

    0 讨论(0)
  • 2020-12-22 06:26

    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

    0 讨论(0)
提交回复
热议问题