EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Access Cookies from a Java Applet


Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html

This Applet uses the package netscape.javascript.JSObject. To compile such program, you have to include in the CLASSPATH the file java40.jar if you have the Netscape Communicator v4 installed or classes.zip for the Microsoft Internet Explorer. Compile with something like


javac -classpath
.;"\program files\netscape\communicator\program\java\classes\java40.jar";%classpath%
TestCookie.java


[HTML file (testCookie.html)]


<HTML><HEAD></HEAD><BODY>
<APPLET CODE=TestCookie.class
MAYSCRIPT
HEIGHT=150
WIDTH=200>
</APPLET></BODY></HTML>


[Java applet (TestCookie.java)]


 import netscape.javascript.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.applet.*;

 public class TestCookie extends Applet 
     implements ActionListener {
   TextField tf1, tf2;
   Button b1, b2, b3;


   public void init() {
     tf1 = new TextField(20);
     tf2 = new TextField(20);
     
     b1 = new Button("Write Cookie");
     b2 = new Button("Read Cookie");
     b3 = new Button("Delete Coookie");
     
     setLayout(new FlowLayout());
     add(tf1);
     add(tf2);
     add(b1);
     add(b2);
     add(b3);
     
     b1.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     }
     
   public void actionPerformed(ActionEvent ae) {
     if (ae.getSource() == b1) {
         /*
         **  computes the expiration date, good for 1 month
         */
         java.util.Calendar c = java.util.Calendar.getInstance();
         c.roll(java.util.Calendar.MONTH, true);
         c.add(java.util.Calendar.MONTH, 1);
         String expires = "; expires=" + c.getTime().toGMTString();

         String s1 = tf1.getText() + expires; 
         JSObject win = JSObject.getWindow(this);
         win.eval("document.cookie=\"" + s1 + "\"");
         }

     if (ae.getSource() == b2) {
         JSObject win = JSObject.getWindow(this);
         String s =(String)win.eval("document.cookie");
         tf2.setText(s);
         /*
           IE4 _may_ have some problem with the eval method, the String is not
           always returned (reported by Ivan Woehr). The workaround is to use
           the getMember method to retrieve the cookie. Here a method to do the
           job nicely:

           String getCookie(String name) {
              JSObject myBrowser = (JSObject) JSObject.getWindow(this);
              JSObject myDocument =  (JSObject) myBrowser.getMember("document");

              String search = name+"==";
              String myCookie = (String)myDocument.getMember("cookie");
              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();
                  return myCookie.substring(offset,end);
                  }
                else 
                 System.out.println("Did not find cookie: "+name);
              return "";
         */
         }

     if (ae.getSource() == b3) {
         /*
         **  to delete a cookie, set the expiration in the past
         */
         java.util.Calendar c = java.util.Calendar.getInstance();
         c.roll(java.util.Calendar.MONTH, true);
         c.add(java.util.Calendar.MONTH, -1);
         String expires = "; expires=" + c.getTime().toGMTString();

         String s1 = tf1.getText() + expires; 
         JSObject win = JSObject.getWindow(this);
         win.eval("document.cookie=\"" + s1 + "\"");
         }
     }
   }




Posted On: 7-Jul-1999

internet.commerce



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy