All Implemented Interfaces:
A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets.
The servlet sends cookies to the browser by using the HttpServletResponse#addCookie method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.
The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest#getCookies method. Several cookies might have the same name but different path attributes.
This class supports both the Version 0 (by Netscape) and Version 1 (by RFC 2109) cookie specifications. By default, cookies are created using Version 0 to ensure the best interoperability.
public Cookie(String name, String value)
The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie's name cannot be changed after creation.
The value can be anything the server chooses to send. Its
value is probably of interest only to the server. The cookie's
value can be changed after creation with the
By default, cookies are created according to the Netscape
cookie specification. The version can be changed with the
|Method from javax.servlet.http.Cookie Summary:|
|clone, getComment, getDomain, getMaxAge, getName, getPath, getSecure, getValue, getVersion, setComment, setDomain, setMaxAge, setPath, setSecure, setValue, setVersion|
|Methods from java.lang.Object:|
|clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait|
|Method from javax.servlet.http.Cookie Detail:|
public String getComment()
public String getDomain()
public int getMaxAge()
public boolean getSecure()
public String getValue()
public int getVersion()
public void setComment(String purpose)
public void setDomain(String pattern)
The form of the domain name is specified by RFC 2109. A domain
name begins with a dot (
public void setMaxAge(int expiry)
A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age.
A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example, /catalog, which makes the cookie visible to all directories on the server under /catalog.
Consult RFC 2109 (available on the Internet) for more information on setting path names for cookies.
public void setSecure(boolean flag)
The default value is
With Version 0 cookies, values should not contain white space, brackets, parentheses, equals signs, commas, double quotes, slashes, question marks, at signs, colons, and semicolons. Empty values may not behave the same way on all browsers.
public void setVersion(int v)
Since RFC 2109 is still somewhat new, consider version 1 as experimental; do not use it yet on production sites.