Cookies are small bits of textual information that the Web server sends to the browser and
that the browser returns unchanged when visiting the same Web site or domain later
A D V E R T I S E M E N T
Advantages of using Cookies
By having the server read information it sent the client previously, the site
can provide visitors with the number of conveniences:
Identifying the user during an e-commerce session. Many on-line
stores use the "shopping cart" metaphor in which the user selects an item,
adds it to his shopping cart, then continues shopping. Since HTTP
connection is closed after each page is sent, when the user select a new
item for his cart, how does the store know that he is the same user that put
the previous item in his cart? Cookies are the good way of accomplishing this.
In fact, this is so useful that servlet have an API specifically for this,
and servlet authors don't need to manipulate cookies directly to make use of
it.
Avoiding username and password. Many large sites require you to
register in order to use their service, but it is inconvenient to remember
the username and password. Cookies are the good alternative for low-security
sites. When a user registers, a cookie is sent with a unique user ID. When
the client reconnects at the later date, the user ID is returned, the server
looks it up, determines it belongs to the registered user, and doesn't require
an explicit username and password.
Customizing a site. Many "portal" sites let you customize the
look of main page. They use cookies to remember what you wanted, so that
you get that result initially for the next time. I'll give an example like this
later in this section of the tutorial.
Focusing advertising. The search engine charge their customers
much more for displaying "directed" ads than "random" ads. That is, if you
do a search on "Java Servlets", a search site can charge much more for an ad
for the servlets development environment than an ad for an on-line travel
agent. On the other hand, if the search had been "Bali Hotels", the
situation would be the reversed. The problem is that they have to show a random
ad when you first arrive and haven't yet performed the search, as well as when
you search on something that doesn't match any ad categories. Cookies let
them remember "Oh, that's the person who was searching for such and such
previously" and displays an appropriate (read "high priced") ad instead of a
random (read "cheap") one.
Creating Cookies
A Cookie is created by calling Cookie constructor, which takes two string: the cookie name and the cookie value
The following example describes how to create a cookie
Cookie userCookie = new Cookie("user", "uid1234");
response.addCookie(userCookie);
Getting the Value of a Cookie with a Specified Name
The following example called cookie.java that slightly simplify the retrieval of a cookie value given a cookie name by looping through the array of available Cookie objects,
returning the value of any Cookie whose name matches the input. If there is no match,
the designated default value is returned. :
public static String getCookieValue(Cookie[] cookies,
String cookieName,
String defaultValue)
{
for(int i=0; i
{
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}