img
user visits an online store. A cookie can save the user 's name, address, and other information.
The user does not need to enter this data each time he or she visits the store.
A servlet can write a cookie to a user 's machine via the addCookie( ) method of the
HttpServletResponse interface. The data for that cookie is then included in the header of
the HTTP response that is sent to the browser.
The names and values of cookies are stored on the user 's machine. Some of the information
that is saved for each cookie includes the following:
·
The name of the cookie
·
The value of the cookie
·
The expiration date of the cookie
·
The domain and path of the cookie
The expiration date determines when this cookie is deleted from the user 's machine. If
an expiration date is not explicitly assigned to a cookie, it is deleted when the current browser
session ends. Otherwise, the cookie is saved in a file on the user 's machine.
The domain and path of the cookie determine when it is included in the header of an
HTTP request. If the user enters a URL whose domain and path match these values, the cookie
is then supplied to the Web server. Otherwise, it is not.
There is one constructor for Cookie. It has the signature shown here:
Cookie(String name, String value)
Here, the name and value of the cookie are supplied as arguments to the constructor. The
methods of the Cookie class are summarized in Table 31-8.
Method
Description
Object clone( )
Returns a copy of this object.
String getComment( )
Returns the comment.
String getDomain( )
Returns the domain.
int getMaxAge( )
Returns the maximum age (in seconds).
String getName( )
Returns the name.
String getPath( )
Returns the path.
boolean getSecure( )
Returns true if the cookie is secure. Other wise, returns false.
String getValue( )
Returns the value.
int getVersion( )
Returns the version.
void setComment(String c)
Sets the comment to c.
void setDomain(String d)
Sets the domain to d.
void setMaxAge(int secs)
Sets the maximum age of the cookie to secs. This is the
number of seconds after which the cookie is deleted.
void setPath(String p)
Sets the path to p.
void setSecure(boolean secure)
Sets the security flag to secure.
void setValue(String v)
Sets the value to v.
void setVersion(int v)
Sets the version to v.
TABLE 31-8
The Methods Defined by Cookie
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home