1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import java.applet.Applet;
import java.util.StringTokenizer; import netscape.javascript.JSObject;
public class CookieTest extends Applet {
public void start() { System.out.println("Saving cookie:"); saveCookie(this, "blah", "captain"); System.out.println("Cookie is:"); System.out.println(loadCookie(this, "blah")); } public String loadCookie(Applet applet, String key) { if (applet != null) { try { applet = lwjglMayscriptFix(applet); String cookie =(String)JSObject.getWindow (applet).eval ("document.cookie"); StringTokenizer st = new StringTokenizer(cookie, ";", false); while (st.hasMoreTokens()) { String psh = st.nextToken().trim(); StringTokenizer z = new StringTokenizer(psh, "=", false); String token = z.nextToken(); if (token.equals(key)) return z.nextToken(); } } catch (Exception e) { System.out.println("not able to load cookie"); } } return null; }
public void saveCookie(Applet applet, String key, String value) { try { applet = lwjglMayscriptFix(applet); JSObject.getWindow (applet).eval ("document.cookie ='" + key + "=" + value + ";expires=Friday, 31-Dec-20 23:59:58 GMT';"); } catch (Exception e) { System.out.println("not able to save cookie"); } } public Applet lwjglMayscriptFix(Applet applet) { if (applet != null) { boolean possibleFix = true; Applet lastApplet = applet; Applet zr; java.util.Enumeration ez = applet.getAppletContext().getApplets(); while (ez.hasMoreElements() && (zr = (Applet)ez.nextElement()) != null) { if (applet != zr) possibleFix = false; lastApplet = zr; } if (possibleFix) applet = lastApplet; } return applet; } } |
Also make sure to include "plugin.jar" into the classpath, it should be located in the same directory as "rt.jar".