| public final class java.net HttpCookie
|
Java SE 6 |
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.
| version | 1.5, 05/11/17 |
| since | 1.6 |
| Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| public | HttpCookie(String name, String value) Details
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
By default, cookies are created according to the RFC 2965
cookie specification. The version can be changed with the
| ||||||||||
| Methods | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| public Object | clone() Details
Create and return a copy of this object.
| ||||||||
| public static boolean | domainMatches(String domain, String host) Details
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 Host A's name domain-matches host B's if:
A host isn't in a domain (RFC 2965 sec. 3.3.2) if:
Examples:
| ||||||||
| public boolean | equals(Object obj) Details
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).
| ||||||||
| public boolean | hasExpired() Details
Reports whether this http cookie has expired or not.
| ||||||||
| public int | hashCode() Details
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()
| ||||||||
| public static List | parse(String header) Details
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.
| ||||||||
| public String | toString() Details
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.
| ||||||||
| Properties | |||||||
|---|---|---|---|---|---|---|---|
| public void | setComment(String purpose) Details
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.
| ||||||
| public String | getComment() Details
Returns the comment describing the purpose of this cookie, or
null if the cookie has no comment.
| ||||||
| public void | setCommentURL(String purpose) Details
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.
| ||||||
| public String | getCommentURL() Details
Returns the comment url describing the purpose of this cookie, or
null if the cookie has no comment url.
| ||||||
| public void | setDiscard(boolean discard) Details
Specify whether user agent should discard the cookie unconditionally.
This is RFC 2965 only attribute.
| ||||||
| public boolean | getDiscard() Details
Return the discard attribute of the cookie
| ||||||
| public void | setDomain(String pattern) Details
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 (
| ||||||
| public String | getDomain() Details
Returns the domain name set for this cookie. The form of
the domain name is set by RFC 2965.
| ||||||
| public void | setMaxAge(long expiry) Details
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.
| ||||||
| public long | getMaxAge() Details
Returns the maximum age of the cookie, specified in seconds.
By default, -1 indicating the cookie will persist
until browser shutdown.
| ||||||
| public String | getName() Details
Returns the name of the cookie. The name cannot be changed after
creation.
| ||||||
| public void | setPath(String uri) Details
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.
| ||||||
| public String | getPath() Details
Returns the path on the server
to which the browser returns this cookie. The
cookie is visible to all subpaths on the server.
| ||||||
| public void | setPortlist(String ports) Details
Specify the portlist of the cookie, which restricts the port(s)
to which a cookie may be sent back in a Cookie header.
| ||||||
| public String | getPortlist() Details
Return the port list attribute of the cookie
| ||||||
| public void | setSecure(boolean flag) Details
Indicates to the browser whether the cookie should only be sent
using a secure protocol, such as HTTPS or SSL.
The default value is
| ||||||
| public boolean | getSecure() Details
Returns true if the browser is sending cookies
only over a secure protocol, or false if the
browser can send cookies using any protocol.
| ||||||
| public void | setValue(String newValue) Details
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.
| ||||||
| public String | getValue() Details
Returns the value of the cookie.
| ||||||
| public void | setVersion(int v) Details
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.
| ||||||
| public int | getVersion() Details
Returns 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.
| ||||||
| About DocWeb · Bundles · Export · Export All | Top 10 · Statistics · Login |
| About Sun · Contact · Privacy · Terms of Use · Trademarks | Java SE 6 · Copyright © 1994-2009 Sun Microsystems, Inc.All rights reserved. Use is subject to license terms |
![]() |
![]() |
|