Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

If you want data stored on the user's station between browser requests, possibly even between sessions, you can use the Cookie class.

A cookie header line is sent by the server to the client's browser, that stores it and sends it back to the server if a later request involves the same URL .

A preferred lifetime of one week is set here, but there's NO GUARANTEE that the cookie will survive that long. Cookies are just text-files that can be deleted by the user/administrator.

In the first request to this servlet's URL, there's no cookie yet, so the servlet creates one to be sent as one of the header lines to the browser.

The essence of a cookie is the data it contains : a name and its associated value.

The value will be encoded, where necessary.

Add a cookie...

AFTER setting the content-type and ...

BEFORE getting the writer !

The browser will send this cookie only to a URL containing the path /pht4 and this example's domain 'localhost'.

==========

NOTES

==========

If you want a cookie to be sent for sub-domains as well, you could specify this ....

for domains like these.

Notice the starting dot !

==========

A cookie's size can be 4KB max, 20 cookies per domain.

==========

To have the browser delete a cookie at once.

==========

A browser may be configured to REFUSE cookies !.

.