Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to store state?

 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a couple of web start apps that would both benefit from being able to store a string (presumably as a cookie). String is approximately 200 char. I saw some weird code that stored data as a cookie but couldn't make head nor tail of it. Is there a simple way? My apps either get called from the browser or from the desktop.

Many thanks for your time

Mike

PS (one app is an arcade game while the other plays chess)
 
Joe Areeda
Ranch Hand
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mich Robinson wrote:I have a couple of web start apps that would both benefit from being able to store a string (presumably as a cookie). String is approximately 200 char. I saw some weird code that stored data as a cookie but couldn't make head nor tail of it. Is there a simple way? My apps either get called from the browser or from the desktop.

Hi Mich,

One of us is missing something. If you use a JNLP you are running on the client's machine and you can just ask for permission to write to the disk, create a file and store whatever you need there.

Now if you need to run in a tight security environment you can have the page that launches the app create a cookie and pass it as a parameter in the jnlp which your app can use to communicate with the server. I don't know of a way for a Java app to access the browser's cookie file and it's complicated because the jnlp file can be stored and launched without a browser.

If all you want to do is store a small amount of information, I would mark the jnlp as needing disk access and put a small file in the $HOME directory.

Joe
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option could be to consider the preference API
 
Mich Robinson
Ranch Hand
Posts: 260
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Areeda wrote:One of us is missing something. If you use a JNLP you are running on the client's machine and you can just ask for permission to write to the disk, create a file and store whatever you need there.
The programs I write are games (arcade games & board games). Users feel fine running a Java app because there is a minimal security risk of viruses etc. If an arcade game pops up a message saying it wants to write to your file system then a fair percentage of potential users will just not want to take the risk. It doesn't really matter that all I wanted to store were high scores and user preferences.

Maneesh Godbole wrote:Another option could be to consider the preference API
I'll look into these. I did try a similar API but I just couldn't get it to work. I'll admit I seem to have a lot more issues trying to run web start applications that applets (I still can't even get the screen icon to show up on the users desktop!). I'll admit I was hoping for a simple pair of methods to call that would write and read a name/value pair (preferably as a cookie or something). Unfortunately nothing is ever as simple as you hope.
 
Jacob Anawalt
Ranch Hand
Posts: 64
Linux Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To store program state between runs of a Java Web Start deployed app without asking for more permissions you could use the javax.jnlp.PersistenceService, a "cookie-like" mechanism. There is also the FileOpenService and FileSaveService under javax.jnlp if you want users to interact with the save mechanism. These interfaces should be available as long as you are running under web start.

I agree that using these interfaces is not simple when compared to a simple java.util.Properties object or the Preferences API. It often seems easier to sign your code and ask for all-permissions the first time it is run so you can use standard persistence methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic