/***** Copyright (c) 1996 Tom Vrankar (twv@ici.net). All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. The original source is available from http://www.ici.net/customers/twv/Java/ THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THIS SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. ----- This applet implements a simple web browser cookie manipulation demonstration. A text field widget is created for the user to enter Set-cookie: records in the Netscape-defined HTTP extension syntax. When the user hits the <Enter> or <Return> key the supplied cookie is presented to the browser. A text area widget displays all cookies currently reported by the browser for this document. How to build: This applet was developed under Windows95, and the following description is Win95-centric; minor translations may be necessary for your environment. First, have the JavaSoft JDK 1.0.2 or later and the Netscape Navigator 3.0b6 or later installed. Next, set your CLASSPATH variable to at least something like: CLASSPATH=.;E:\Interpreters\java\lib\classes.zip;E:\Browsers\Netscape\Navigator\Program\java\classes\java_30 In other words, a minimum of ".", the JDK "classes.zip", and the Navigator "java_30" components. I did not use the LiveConnect SDK. Remember that since the JDK is all 32-bit, you don't put double-quotes around any long path names in CLASSPATH. From there it's just "javac -O cookies.java". The whole demo fits into one class. How to invoke: This applet relies on Netscape's LiveConnect feature in order to provide Java applets access to JavaScript. At this time, only JavaScript has defined access to cookies -- no standard yet exists for Java to directly access cookies. In order to turn on Java to JavaScript communication, the HTML <applet> tag must include the following attributes: <applet code="cookies.class" width=500 height=300 mayscript=true>Got Java?</applet> You'll notice in the following code that magic Netscape classes are referenced to access JavaScript. Does this mean it can't be done from Microsoft Internet Explorer? Well, does it? twv@ici.net http://www.ici.net/customers/twv/*****/import java.io.*;import java.util.*;import java.awt.*;import java.applet.*;import netscape.javascript.*;import netscape.javascript.JSObject;import netscape.javascript.JSException;public class cookies extends Applet { TextField tf =null; TextArea ta =null; // init: assemble the UI public void init() {  Panel ptf =new Panel();  Label ltf =new Label (" Set-cookie: ", Label.RIGHT);  tf =new TextField();  Label lta =new Label ("Available Cookies:", Label.CENTER);  ptf.setLayout (new BorderLayout (1, 3));  ptf.add ("West", ltf);  ptf.add ("South", lta);  ptf.add ("Center", tf);  ta =new TextArea (16, 80);  ta.setEditable (false);  setLayout (new BorderLayout (3, 3));  add ("North", ptf);  add ("Center", ta); } // start: HOW TO READ COOKIES and update the textarea public void start() {  StringBuffer cookietext =new StringBuffer();  try {   String linesep =System.getProperty ("line.separator");   String cookie =(String)JSObject.getWindow (this).eval ("document.cookie");   StringTokenizer st = new StringTokenizer(cookie, ";", false);   // peel apart the cookies   while (st.hasMoreTokens()) {    cookietext.append (st.nextToken().trim() +linesep);   }  }  catch (Exception ex) {   cookietext.append ("This browser may not support Java to Javascript communication");  }  ta.setText (cookietext.toString()); } // action: HOW TO WRITE COOKIES from the textfield contents into the browser public boolean action (Event ev, Object arg) {  if (ev.target ==tf) {   try {    if (((String)arg).length() !=0) {     JSObject.getWindow (this).eval ("document.cookie ='" +(String)arg +"';");    }    start(); // refresh the textarea   }   catch (Exception ex) {    ta.setText (ex.getMessage());   }   return true;  }  else {   return false;  } }}