/** * Read a cookie * * @param name The name of the cookie * @return The value of the cookie, if the cookie does not exist it will return an empty string */ protected String getCookie(String name) { /* ** get a specific cookie by its name, parse the cookie. ** not used in this Applet but can be useful */ String myCookie = getCookie(); String search = name + "="; if (myCookie.length() > 0) { int offset = myCookie.indexOf(search); if (offset != -1) { offset += search.length(); int end = myCookie.indexOf(";", offset); if (end == -1) { end = myCookie.length(); } Log.println(this, "get: " + myCookie.substring(offset, end)); if (!myCookie.substring(offset, end).equals(" ")) { return myCookie.substring(offset, end); } else { return ""; } } else Log.println(this, "Did not find cookie: " + name); } return ""; }
/** * Write a cookie * * @param name The name of the cookie * @param value The value to write */ protected void setCookie(String name, String value) { value = cleanValue(value); /* ** write a cookie ** computes the expiration date, good for 1 month */ java.util.Calendar c = java.util.Calendar.getInstance(); c.add(java.util.Calendar.YEAR, 1); String expires = "; expires=" + c.getTime().toString(); String s1 = name + "=" + value + expires; Log.println(this, s1); try { JSObject myBrowser = JSObject.getWindow(applet); JSObject myDocument = (JSObject) myBrowser.getMember("document"); myDocument.setMember("cookie", s1); Log.println(this, "set:" + s1); } catch (JSException e) { e.printStackTrace(); } }
/** * Delete a cookie * * @param name The name of the cookie */ protected void deleteCookie(String name) { setCookie(name, " "); /* ** delete a cookie, set the expiration in the past */ java.util.Calendar c = java.util.Calendar.getInstance(); c.add(java.util.Calendar.MONTH, -1); String expires = "; expires=" + c.getTime().toString(); String s1 = name + expires; try { JSObject myBrowser = JSObject.getWindow(applet); JSObject myDocument = (JSObject) myBrowser.getMember("document"); Log.println(this, "del: " + s1); myDocument.setMember("cookie", s1); } catch (JSException e) { e.printStackTrace(); } }