Before adding the cookie to the outgoing headers, you can look up or set attributes of the cookie. Here’s a summary:
- Gets/sets a comment associated with this cookie.
- Gets/sets the domain to which cookie applies. Normally, cookies are returned only to the exact hostname that sent them. You can use this method to instruct the browser to return them to other hosts within the same domain. Note that the domain should start with a dot (e.g. .
prenhall.com), and must contain two dots for non-country domains like
.gov, and three dots for country domains like
- Gets/sets how much time (in seconds) should elapse before the cookie expires. If you don’t set this, the cookie will last only for the current session (i.e. until the user quits the browser), and will not be stored on disk. See the
LongLivedCookieclass below, which defines a subclass of
Cookiewith a maximum age automatically set one year in the future.
- Gets/sets the name of the cookie. The name and the value are the two pieces you virtually always care about. Since the
HttpServletRequestreturns an array of
Cookieobjects, it is common to loop down this array until you have a particular name, then check the value with getValue. See the
getCookieValuemethod shown below.
- Gets/sets the path to which this cookie applies. If you don’t specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories. This method can be used to specify something more general. For example,
someCookie.setPath("/")specifies that all pages on the server should receive the cookie. Note that the path specified must include the current directory.
- Gets/sets the
booleanvalue indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections.
- Gets/sets the value associated with the cookie. Again, the name and the value are the two parts of a cookie that you almost always care about, although in a few cases a name is used as a boolean flag, and its value is ignored (i.e the existence of the name means true).
- Gets/sets the cookie protocol version this cookie complies with. Version 0, the default, adheres to the original Netscape specification. Version 1, not yet widely supported, adheres to RFC 2109