Storing Client State in Applets

I want to store my client state in cookies in the browser for Putty. There didn’t seem to be a way to set cookies client side so I wrote a PHP script that the client calls the return from which sets the cookie, this isn’t going to work in the future (some places I’d like to host can’t support PHP). So, is there a way to set cookies client side without an external URL?

Kev

applet.showDocument(“javascript:myFunction();”);

Where myFunction() does what you want.

— this does NOT work in Opera — it doesn’t understand the ‘javascript:’ protocol

A heck of a lot more stable is this:
applet.showDocument(“javascript:eval(hexToStr(’”+strToHex(“myFunction();”)+"’));");

You have to implement strToHex and hexToStr yourself though.

The workaround in Opera is to have this iframe in your page…
applet.showDocument(“http://mysite.com/runscript.php?eval_hex="+strToHex("myFunction();”), “myIframeTarget”);
where runscript.php reads the argument, decodes it with hexToStr and writes <script …> around it.

I use this to communicate between a webpage and an applet (both ways) without LiveConnect (which is buggy in FF)

what about using

JSObject window = JSObject.getWindow(appletinstance); 
window.eval("myFunction();");

?

Been meaning to ask about this JSObject stuff, is that compatible everywhere? Seemed to be borked on my IE.

Kev

Is this any help at all?

http://java.sun.com/j2se/1.5.0/docs/guide/deployment/deployment-guide/cookie_support.html

I’m kinda wondering how to best do this as well…

Yeah, went through that. The cookie management thing requires signed JARs - hence the PHP hack.

Kev

Isn’t that just the two last examples (using CookieHandler)? The first two seems to do things manually via URLConnections.

I can’t see how the first two actually let you store and retrieve values as cookies though. The server has to be set up to set the cookie on the response doesn’t it (which is where my PHP came in)?

Kev

Oh, I thought it was some kind of standard HTTP stuff. That’s a shame.

Flash (and Silverlight) has some really nice limited local storage stuff that’s even cross platform so you can get the same “cookies” on the same computer regardless of what browser you use. Java needs something like that.

Yeah, I was kinda hoping the Java Preferences API was going to be it, but I seem to remember that was security manager restricted aswell.

Kev

Actually, if that JSObject is supported everywhere couldn’t we use:


// get cookies
JSObject document = (JSObject) window.getMember("document");
String load = (String) document.getMember("cookie");

// set cookies
document.setMember("cookie", "x=y; x2=y2");

Kev

It seems it’s there since 1.3
http://java.sun.com/products/plugin/1.3/docs/jsobject.html

The problem isn’t the java version, but the quality of the implementation might suck?

This might sound arrogant, but I don’t understand why you’d look any further than my solution…

It works for me, in all browsers, even Safari.

Watch out for the FF bug in 1.6.0_03 - 1.6.0_09 (fixed in update 10), where using LiveConnect causes problems: http://bugs.sun.com/view_bug.do?bug_id=6669818

Web browsers may have the following limitations for cookies (according to RFC 2109 section 6.3):

  • 300 cookies total
  • 20 cookies per domain (per site, not per page)
  • 4,096 bytes per cookie (name and value combined)
  • Additionally, Internet Explorer allows only 4,096 bytes per domain (!)

Also of course cookies can have limitations on the characters they can use. An easy way around that is to encode data as Base64.

Since there are only 20 cookies per domain, what I do is only store one cookie per game, and that cookie is a Base64-encoded gzipped byte array of tons of data (name, previous scores, sound on/off, etc).

In 6u10, the JNLP API can be used (PersistenceService).

Maybe I read it wrong - the first bit you said didn’t work in Opera. The second bit (Opera workaround) relies on PHP (which isn’t available on a site where I’m going to host - it also can’t link out to somewhere that does use PHP).

Thanks for the details brackeen - how do you go about setting cookies atm?

Kev

Well, it can be implemented by PHP…

In javascript you can figure out your own URI using window.location.href (including the part after the ‘?’) so you can put your decoder+eval(…) in your body.onLoad

Problem solved.

Using JSObject (via reflection), but Riven’s idea looks interesting for setting cookies… it would be a good workaround for the FF LiveConnect bug.

Riven, how to you get cookies via this method?

Cheers. So I could use that method in all cases?

Kev

Read document.cookie.
Use DOM to insert it into the
poll the applet.getParameter(“myCookie”) in the Applet for change.