public class Cookie
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
Cookies are named, and have a single value. They may have optional attributes, including a comment presented to the user, path and domain qualifiers for which hosts see the cookie, a maximum age, and a version. Current web browsers often have bugs in how they treat those attributes, so interoperability can be improved by not relying on them heavily.
Cookies are assigned by servers, using fields added to HTTP response headers. Cookies are passed back to those servers using fields added to HTTP request headers. Several cookies with the same name can be returned; they have different path attributes, but those attributes will not be visible when using "old format" cookies.
Cookies affect the caching of the web pages used to set their values. At this time, none of the sophisticated HTTP/1.1 cache control models are supported. Standard HTTP/1.0 caches will not cache pages which contain cookies created by this class.
Cookies are being standardized by the IETF. This class supports the original Cookie specification (from Netscape Communications Corp.) as well as the updated RFC 2109 specification.
构造器和说明 |
---|
Cookie(java.lang.String name,
java.lang.String value)
Defines a cookie with an initial name/value pair.
|
限定符和类型 | 方法和说明 |
---|---|
java.lang.Object |
clone()
Returns a copy of this object.
|
java.lang.String |
getComment()
Returns the comment describing the purpose of this cookie, or null if no
such comment has been defined.
|
java.lang.String |
getDomain()
Returns the domain of this cookie.
|
java.util.Date |
getExpiryDate()
Returns the expiry date of the cookie.
|
java.lang.String |
getName()
Returns the name of the cookie.
|
java.lang.String |
getPath()
Returns the prefix of all URLs for which this cookie is targetted.
|
boolean |
getSecure()
Returns the value of the 'secure' flag.
|
java.lang.String |
getValue()
Returns the value of the cookie.
|
int |
getVersion()
Returns the version of the cookie.
|
void |
setComment(java.lang.String purpose)
If a user agent (web browser) presents this cookie to a user, the
cookie's purpose will be described using this comment.
|
void |
setDomain(java.lang.String pattern)
This cookie should be presented only to hosts satisfying this domain name
pattern.
|
void |
setExpiryDate(java.util.Date expiry)
Sets the expiry date of the cookie.
|
void |
setPath(java.lang.String uri)
This cookie should be presented only with requests beginning with this
URL.
|
void |
setSecure(boolean flag)
Indicates to the user agent that the cookie should only be sent using a
secure protocol (https).
|
void |
setValue(java.lang.String newValue)
Sets the value of the cookie.
|
void |
setVersion(int version)
Sets the version of the cookie protocol used when this cookie saves
itself.
|
java.lang.String |
toString()
Convert this cookie into a user friendly string.
|
public Cookie(java.lang.String name, java.lang.String value) throws java.lang.IllegalArgumentException
name
- The name of the cookie.value
- The value of the cookie.java.lang.IllegalArgumentException
- if the cookie name is not an HTTP/1.1 "token", or if it is
one of the tokens reserved for use by the cookie protocolpublic void setComment(java.lang.String purpose)
purpose
- The cookie comment.getComment()
public java.lang.String getComment()
null
if none.setComment(java.lang.String)
public void setDomain(java.lang.String pattern)
pattern
- The domain name pattern. The pattern is converted to
lower case to accommodate less capable browsers.getDomain()
public java.lang.String getDomain()
setDomain(java.lang.String)
public void setExpiryDate(java.util.Date expiry)
expiry
- The expiry date for this cookie, or null
if
the cookie is persistent.getExpiryDate()
public java.util.Date getExpiryDate()
null
if it is persistent.setExpiryDate(java.util.Date)
public void setPath(java.lang.String uri)
uri
- The exclusion prefix for the cookie.getPath()
public java.lang.String getPath()
setPath(java.lang.String)
public void setSecure(boolean flag)
flag
- Use true
if the cookie is to be sent using
secure protocols, false
otherwise.getSecure()
public boolean getSecure()
true
if this cookie should only be sent using
a secure protocol, false
otherwise.setSecure(boolean)
public java.lang.String getName()
public void setValue(java.lang.String newValue)
With version zero cookies, you need to be careful about the kinds of values you use. Values with various special characters (whitespace, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the "at" sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers.
newValue
- The new value for the cookie.getValue()
public java.lang.String getValue()
setValue(java.lang.String)
public int getVersion()
setVersion(int)
public void setVersion(int version)
version
- The version of the cookie, either 0 or 1.getVersion()
public java.lang.Object clone()
clone
在类中 java.lang.Object
public java.lang.String toString()
toString
在类中 java.lang.Object