java.lang.Object | |
↳ | java.net.HttpCookie |
An HttpCookie object represents an http cookie, which carries state information between server and user agent. Cookie is widely adopted to create stateful sessions.
There are 3 http cookie specifications:
Netscape draft
RFC 2109 - http://www.ietf.org/rfc/rfc2109.txt
RFC 2965 - http://www.ietf.org/rfc/rfc2965.txt
HttpCookie class can accept all these 3 forms of syntax.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a cookie with a specified name and value.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Create and return a copy of this object.
| |||||||||||
The utility method to check whether a host name is in a domain
or not.
| |||||||||||
Test the equality of two http cookies.
| |||||||||||
Returns the comment describing the purpose of this cookie, or
null if the cookie has no comment. | |||||||||||
Returns the comment url describing the purpose of this cookie, or
null if the cookie has no comment url. | |||||||||||
Return the discard attribute of the cookie
| |||||||||||
Returns the domain name set for this cookie.
| |||||||||||
Returns the maximum age of the cookie, specified in seconds.
| |||||||||||
Returns the name of the cookie.
| |||||||||||
Returns the path on the server
to which the browser returns this cookie.
| |||||||||||
Return the port list attribute of the cookie
| |||||||||||
Returns
true if the browser is sending cookies
only over a secure protocol, or false if the
browser can send cookies using any protocol. | |||||||||||
Returns the value of the cookie.
| |||||||||||
Returns the version of the protocol this cookie complies
with.
| |||||||||||
Reports whether this http cookie has expired or not.
| |||||||||||
Return hash code of this http cookie.
| |||||||||||
Constructs cookies from set-cookie or set-cookie2 header string.
| |||||||||||
Specifies a comment that describes a cookie's purpose.
| |||||||||||
Specifies a comment url that describes a cookie's purpose.
| |||||||||||
Specify whether user agent should discard the cookie unconditionally.
| |||||||||||
Specifies the domain within which this cookie should be presented.
| |||||||||||
Sets the maximum age of the cookie in seconds.
| |||||||||||
Specifies a path for the cookie
to which the client should return the cookie.
| |||||||||||
Specify the portlist of the cookie, which restricts the port(s)
to which a cookie may be sent back in a Cookie header.
| |||||||||||
Indicates to the browser whether the cookie should only be sent
using a secure protocol, such as HTTPS or SSL.
| |||||||||||
Assigns a new value to a cookie after the cookie is created.
| |||||||||||
Sets the version of the cookie protocol this cookie complies
with.
| |||||||||||
Constructs a cookie header string representation of this cookie,
which is in the format defined by corresponding cookie specification,
but without the leading "Cookie:" token.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Constructs a cookie with a specified name and value.
The name must conform to RFC 2965. 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
setValue
method.
By default, cookies are created according to the RFC 2965
cookie specification. The version can be changed with the
setVersion
method.
name | a String specifying the name of the cookie |
---|---|
value | a String specifying the value of the cookie |
IllegalArgumentException | if the cookie name contains illegal characters or it is one of the tokens reserved for use by the cookie protocol |
---|---|
NullPointerException | if name is null |
The utility method to check whether a host name is in a domain or not.
This concept is described in the cookie specification. To understand the concept, some terminologies need to be defined first:
effective host name = hostname if host name contains dot
or = hostname.local if not
Host A's name domain-matches host B's if:
- their host name strings string-compare equal; or
- A is a HDN string and has the form NB, where N is a non-empty name string, B has the form .B', and B' is a HDN string. (So, x.y.com domain-matches .Y.com but not Y.com.)
A host isn't in a domain (RFC 2965 sec. 3.3.2) if:
- The value for the Domain attribute contains no embedded dots, and the value is not .local.
- The effective host name that derives from the request-host does not domain-match the Domain attribute.
- The request-host is a HDN (not IP address) and has the form HD, where D is the value of the Domain attribute, and H is a string that contains one or more dots.
Examples:
- A Set-Cookie2 from request-host y.x.foo.com for Domain=.foo.com would be rejected, because H is y.x and contains a dot.
- A Set-Cookie2 from request-host x.foo.com for Domain=.foo.com would be accepted.
- A Set-Cookie2 with Domain=.com or Domain=.com., will always be rejected, because there is no embedded dot.
- A Set-Cookie2 with Domain=ajax.com will be accepted, and the value for Domain will be taken to be .ajax.com, because a dot gets prepended to the value.
- A Set-Cookie2 from request-host example for Domain=.local will be accepted, because the effective host name for the request- host is example.local, and example.local domain-matches .local.
domain | the domain name to check host name with |
---|---|
host | the host name in question |
Test the equality of two http cookies.
The result is true only if two cookies come from same domain (case-insensitive), have same name (case-insensitive), and have same path (case-sensitive).
obj | the reference object with which to compare. |
---|
Returns the comment describing the purpose of this cookie, or
null
if the cookie has no comment.
String
containing the comment,
or null
if noneReturns the comment url describing the purpose of this cookie, or
null
if the cookie has no comment url.
String
containing the comment url,
or null
if noneReturn the discard attribute of the cookie
Returns the domain name set for this cookie. The form of the domain name is set by RFC 2965.
String
containing the domain nameReturns the maximum age of the cookie, specified in seconds.
By default, -1
indicating the cookie will persist
until browser shutdown.
Returns the name of the cookie. The name cannot be changed after creation.
String
specifying the cookie's name
Returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server.
String
specifying a path that contains
a servlet name, for example, /catalogReturn the port list attribute of the cookie
Returns true
if the browser is sending cookies
only over a secure protocol, or false
if the
browser can send cookies using any protocol.
true
if the browser can use
any standard protocol; otherwise, false
Returns the value of the cookie.
String
containing the cookie's
present valueReturns the version of the protocol this cookie complies with. Version 1 complies with RFC 2965/2109, and version 0 complies with the original cookie specification drafted by Netscape. Cookies provided by a browser use and identify the browser's cookie version.
Reports whether this http cookie has expired or not.
Return hash code of this http cookie. The result is the sum of hash code value of three significant components of this cookie: name, domain, and path. That is, the hash code is the value of the expression:
getName().toLowerCase().hashCode()
+ getDomain().toLowerCase().hashCode()
+ getPath().hashCode()
Constructs cookies from set-cookie or set-cookie2 header string. RFC 2965 section 3.2.2 set-cookie2 syntax indicates that one header line may contain more than one cookie definitions, so this is a static utility method instead of another constructor.
header | a String specifying the set-cookie header. The header should start with "set-cookie", or "set-cookie2" token; or it should have no leading token at all. |
---|
IllegalArgumentException | if header string violates the cookie specification's syntax, or the cookie name contains llegal characters, or the cookie name is one of the tokens reserved for use by the cookie protocol |
---|---|
NullPointerException | if the header string is null |
Specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user. Comments are not supported by Netscape Version 0 cookies.
purpose | a String specifying the comment
to display to the user |
---|
Specifies a comment url that describes a cookie's purpose. The comment url is useful if the browser presents the cookie to the user. Comment url is RFC 2965 only.
purpose | a String specifying the comment url
to display to the user |
---|
Specify whether user agent should discard the cookie unconditionally. This is RFC 2965 only attribute.
discard | true indicates to discard cookie unconditionally |
---|
Specifies the domain within which this cookie should be presented.
The form of the domain name is specified by RFC 2965. A domain
name begins with a dot (.foo.com
) and means that
the cookie is visible to servers in a specified Domain Name System
(DNS) zone (for example, www.foo.com
, but not
a.b.foo.com
). By default, cookies are only returned
to the server that sent them.
pattern | a String containing the domain name
within which this cookie is visible;
form is according to RFC 2965 |
---|
Sets the maximum age of the cookie in seconds.
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.
expiry | an integer specifying the maximum age of the cookie in seconds; if zero, the cookie should be discarded immediately; otherwise, the cookie's max age is unspecified. |
---|
Specifies a path for the cookie to which the client should return the cookie.
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 2965 (available on the Internet) for more information on setting path names for cookies.
uri | a String specifying a path |
---|
Specify the portlist of the cookie, which restricts the port(s) to which a cookie may be sent back in a Cookie header.
ports | a String specify the port list, which is comma seperated series of digits |
---|
Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.
The default value is false
.
flag | if true , sends the cookie from the browser
to the server using only when using a secure protocol;
if false , sent on any protocol |
---|
Assigns a new value to a cookie after the cookie is created. If you use a binary value, you may want to use BASE64 encoding.
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.
newValue | a String specifying the new value |
---|
Sets the version of the cookie protocol this cookie complies with. Version 0 complies with the original Netscape cookie specification. Version 1 complies with RFC 2965/2109.
v | 0 if the cookie should comply with the original Netscape specification; 1 if the cookie should comply with RFC 2965/2109 |
---|
IllegalArgumentException | if v is neither 0 nor 1 |
---|
Constructs a cookie header string representation of this cookie, which is in the format defined by corresponding cookie specification, but without the leading "Cookie:" token.