Java Glossary : cookie

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : C words : cookie.

cookie
A cookie is a technique a CGI server can use to store information in an HTTP client for later retrieval to remind itself where it was in dealing with the client. You could think of a cookie as a laundry ticket that the client gives back to the server to remind it who the client is and what the client and server have done together in the past. You can see the cookies you have accumulated in your surfing in a file called: Program Files\Netscape\Users\roedy\cookies.txt. Here is what a typical cookie looks like inside the cookies.txt file: That cookie allows Netscape.com to remember my old email address, roedy@mindprod.com from the last time I told them when I visited. The rest of the cookie is somewhat incomprehensible, but it contains encoded information about me that the Netscape server finds useful. A shopping cart web site might encode the contents of my shopping cart as a series of cookies. The HTTP format of cookies in the HTTP header is somewhat more human-readable. e.g.

The browser records the cookies with the site they came from, an expiry date, the cookie name and its value. They are exchanged quietly between the browser and the server without displaying them on the screen. Every time your browser makes a request of a domain/path for which it has a cookie, it automatically piggybacks the cookie in the HTTP Cookie: request header line. The host does not have to request it.

Applets too can play with cookies, but only with the ones put there by the server from which they were loaded, or that were put there by Applets from that server. Applets can both read and write the cookies stored in the browser, just as the server can.

Tom Hall provided the following code snippet to allow an Applet to write a new cookie or to read the cookies it is permitted to see. To delete a cookie, rewrite it with an expiry date in the past. Surprisingly, even though this code makes use of the Netscape JSObject, it works fine in Internet Explorer as well - no need to include the JSObject class files as they are packaged with IE's Virtual Machine. For it to work, your APPLET tag must include the MAYSCRIPT option.

You will also have to put the file \program files\netscape\communicator\program\java\classes\java40.jar on the classpath when you are compiling. The IE browser will deal with finding the classes at run time. Unfortunately, the only browser I found that supports this is Internet Explorer 5.5/6.0.26. Not even Netscape 4.79 works!


view

book_coverCore Web Programming, Second Edition
0-13-089793-0
Marty Hall and Gary Cornell
1250 pages. This is a great doorstop of a book. It has a few chapters on client-server programming in Java, and a section of that is on CGI. I have looked at hundreds of Java books and found nothing that deals in depth with client side Java talking to CGI, except Marty's book. It is really very simple and he does an excellent job of explaining it. Marty has posted all the source code examples from the book for anyone to use. These contain updates and errata fixes you don't get on the CD-ROM that comes with the book.
amazon.com Barnes and Noble
amazon.ca chapters
amazon.co.uk amazon.de
explains cookies clearly, and shows you how to write server code to read and write the cookies in your visitor's browsers.

I have only been able to get cookies to work in IE 5.5, not Netscape or Opera. Perhaps it would be best to try with JavaScript read/write cookie functions.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 2757.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/cookie.html J:\mindprod\jgloss\cookie.html