|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
javax.servlet.http Class Cookie
java.lang.Object javax.servlet.http.Cookie
- All Implemented Interfaces:
- Cloneable
创建一个 cookie,cookie 是 servlet 发送到 Web 浏览器的少量信息,这些信息由浏览器保存,然后发送回服务器。cookie 的值可以唯一地标识客户端,因此 cookie 常用于会话管理。
一个 cookie 拥有一个名称、一个值和一些可选属性,比如注释、路径和域限定符、最大生存时间和版本号。一些 Web 浏览器在处理可选属性方面存在 bug,因此有节制地使用这些属性可提高 servlet 的互操作性。
servlet 通过使用 HttpServletResponse#addCookie
方法将 cookie 发送到浏览器,该方法将字段添加到 HTTP 响应头,以便一次一个地将 cookie 发送到浏览器。浏览器应该支持每台 Web 服务器有 20 个 cookie,总共有 300 个 cookie,并且可能将每个 cookie 的大小限定为 4 KB。
浏览器通过向 HTTP 请求头添加字段将 cookie 返回给 servlet。可使用 HttpServletRequest#getCookies
方法从请求中获取 cookie。一些 cookie 可能有相同的名称,但却有不同的路径属性。
cookie 影响使用它们的 Web 页面的缓存。HTTP 1.0 不会缓存那些使用通过此类创建的 cookie 的页面。此类不支持 HTTP 1.1 中定义的缓存控件。
此类支持版本 0(遵守 Netscape 协议)和版本 1(遵守 RFC 2109 协议)cookie 规范。默认情况下,cookie 是使用版本 0 创建的,以确保最佳互操作性。
Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.
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(javax.servlet.http.Cookie)
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.
Cookies affect the caching of the Web pages that use them. HTTP 1.0 does not cache pages that use cookies created with this class. This class does not support the cache control defined with HTTP 1.1.
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.
- Author:
- Various
Constructor Summary | |
---|---|
Cookie(String name,
String value)
Constructs a cookie with a specified name and value. |
Method Summary | |
---|---|
Object |
clone()
Overrides the standard java.lang.Object.clone
method to return a copy of this cookie. |
String |
getComment()
Returns the comment describing the purpose of this cookie, or null if the cookie has no comment. |
String |
getDomain()
Returns the domain name set for this cookie. |
int |
getMaxAge()
Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist
until browser shutdown. |
String |
getName()
Returns the name of the cookie. |
String |
getPath()
Returns the path on the server to which the browser returns this cookie. |
boolean |
getSecure()
Returns true if the browser is sending cookies
only over a secure protocol, or false if the
browser can send cookies using any protocol. |
String |
getValue()
Returns the value of the cookie. |
int |
getVersion()
Returns the version of the protocol this cookie complies with. |
void |
setComment(String purpose)
Specifies a comment that describes a cookie's purpose. |
void |
setDomain(String pattern)
Specifies the domain within which this cookie should be presented. |
void |
setMaxAge(int expiry)
Sets the maximum age of the cookie in seconds. |
void |
setPath(String uri)
Specifies a path for the cookie to which the client should return the cookie. |
void |
setSecure(boolean flag)
Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL. |
void |
setValue(String newValue)
Assigns a new value to a cookie after the cookie is created. |
void |
setVersion(int v)
Sets the version of the cookie protocol this cookie complies with. |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public
Cookie(String name, String value)
构造带指定名称和值的 cookie。
名称必须遵守 RFC 2109。这意味着它只能包含 ASCII 字母数字字符,不能包含逗号、分号或空格,也不能以 $ 字符开头。cookie 的名称在创建之后不得更改。
该值可以是服务器选择发送的任何值。可能只有该服务器需要其值。cookie 的值在使用 setValue
方法创建后可以更改。
默认情况下,根据 Netscape cookie 规范创建 cookie。可以使用 setVersion
方法更改版本。
name |
指定 cookie 名称的 String |
value |
指定 cookie 值的 String |
Throws | IllegalArgumentException: 如果 cookie 名称包含非法字符(例如,逗号、空格或分号),或者它是为了供 cookie 协议使用而保留的标记之一 |
See also | setValue, setVersion |
Cookie
public Cookie(String name, String value)
- Constructs a cookie with a specified name and 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
setValue
method.By default, cookies are created according to the Netscape cookie specification. The version can be changed with the
setVersion
method.- Parameters:
name
- aString
specifying the name of the cookievalue
- aString
specifying the value of the cookie- Throws:
IllegalArgumentException
- if the cookie name contains illegal characters (for example, a comma, space, or semicolon) or it is one of the tokens reserved for use by the cookie protocol- See Also:
setValue(java.lang.String)
,setVersion(int)
Method Detail |
---|
public void
setComment(String purpose)
指定一个描述 cookie 用途的注释。如果浏览器向用户显示 cookie,则注释很有用。Netscape Version 0 cookie 不支持注释。
purpose |
一个 String ,指定要显示给用户的注释 |
See also | getComment |
setComment
public void setComment(String purpose)
- 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.
- Parameters:
purpose
- aString
specifying the comment to display to the user- See Also:
getComment()
public String
getComment()
返回描述此 cookie 用途的注释;如果该 cookie 没有注释,则返回 null
。
return |
包含注释的 String ;如果没有注释,则返回 null
|
See also | setComment |
getComment
public String getComment()
- Returns the comment describing the purpose of this cookie, or
null
if the cookie has no comment. - Returns:
- a
String
containing the comment, ornull
if none - See Also:
setComment(java.lang.String)
public void
setDomain(String pattern)
指定应在其中显示此 cookie 的域。
RFC 2109 指定了域名的形式。域名以点 (.foo.com
) 开头,意味着在指定域名系统(Domain Name System,DNS)区域中(例如,www.foo.com
,但不是 a.b.foo.com
)cookie 对于服务器是可见的。默认情况下,cookie 只返回给发送它们的服务器。
pattern |
包含域名(在其中此 cookie 可见)的 String ;域名形式符合 RFC 2109 |
See also | getDomain |
setDomain
public void setDomain(String pattern)
- Specifies the domain within which this cookie should be presented.
The form of the domain name is specified by RFC 2109. 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 nota.b.foo.com
). By default, cookies are only returned to the server that sent them. - Parameters:
pattern
- aString
containing the domain name within which this cookie is visible; form is according to RFC 2109- See Also:
getDomain()
public String
getDomain()
返回为此 cookie 设置的域名。域名形式是根据 RFC 2109 设置的。
return |
包含域名的 String
|
See also | setDomain |
getDomain
public String getDomain()
- Returns the domain name set for this cookie. The form of
the domain name is set by RFC 2109.
- Returns:
- a
String
containing the domain name - See Also:
setDomain(java.lang.String)
public void
setMaxAge(int expiry)
设置 cookie 的最大生存时间,以秒为单位。
正值表示 cookie 将在经过该值表示的秒数后过期。注意,该值是 cookie 过期的最大 生存时间,不是 cookie 的当前生存时间。
负值意味着 cookie 不会被持久存储,将在 Web 浏览器退出时删除。0 值会导致删除 cookie。
expiry | 指定 cookie 的最大生存时间(以秒为单位)的整数;如果为负数,则表示不存储该 cookie;如果为 0,则删除该 cookie |
See also | getMaxAge |
setMaxAge
public void setMaxAge(int expiry)
- 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.
- Parameters:
expiry
- an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookie- See Also:
getMaxAge()
public int
getMaxAge()
返回以秒为单位指定的 cookie 的最大生存时间,默认情况下,-1
指示该 cookie 将保留到浏览器关闭为止。
return | 指定 cookie 的最大生存时间(以秒为单位)的整数;如果为负数,则表示该 cookie 保留到浏览器关闭为止 |
See also | setMaxAge |
getMaxAge
public int getMaxAge()
- Returns the maximum age of the cookie, specified in seconds,
By default,
-1
indicating the cookie will persist until browser shutdown. - Returns:
- an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie persists until browser shutdown
- See Also:
setMaxAge(int)
public void
setPath(String uri)
指定客户端应该返回 cookie 的路径。
cookie 对于指定目录中的所有页面及该目录子目录中的所有页面都是可见的。cookie 的路径必须包括设置 cookie 的 servlet,例如 /catalog,它使 cookie 对于服务器上 /catalog 下的所有目录都是可见的。
有关设置 cookie 路径名称的更多信息,请参考 RFC 2109(可从 Internet 上获得)。
uri |
指定路径的 String |
See also | getPath |
setPath
public void setPath(String uri)
- 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 2109 (available on the Internet) for more information on setting path names for cookies.
- Parameters:
uri
- aString
specifying a path- See Also:
getPath()
public String
getPath()
返回浏览器将此 cookie 返回到的服务器上的路径。cookie 对于服务器上的所有子路径都是可见的。
return |
指定包含 servlet 名称的路径的 String ,例如 /catalog
|
See also | setPath |
getPath
public String getPath()
- Returns the path on the server
to which the browser returns this cookie. The
cookie is visible to all subpaths on the server.
- Returns:
- a
String
specifying a path that contains a servlet name, for example, /catalog - See Also:
setPath(java.lang.String)
public void
setSecure(boolean flag)
指示浏览器是否只能使用安全协议(如 HTTPS 或 SSL)发送 cookie。
默认值为 false
。
flag |
如果为 true ,则仅在使用安全协议时将 cookie 从浏览器发送到服务器;如果为 false ,则在使用任何协议时都可以发送 |
See also | getSecure |
setSecure
public void setSecure(boolean flag)
- 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
. - Parameters:
flag
- iftrue
, sends the cookie from the browser to the server only when using a secure protocol; iffalse
, sent on any protocol- See Also:
getSecure()
public boolean
getSecure()
如果浏览器仅通过安全协议发送 cookie,则返回 true
;如果浏览器可以使用任何协议发送 cookie,则返回 false
。
return |
如果浏览器使用的是安全协议,则返回 true ;否则返回 false |
See also | setSecure |
getSecure
public boolean getSecure()
- Returns
true
if the browser is sending cookies only over a secure protocol, orfalse
if the browser can send cookies using any protocol. - Returns:
true
if the browser uses a secure protocol; otherwise,true
- See Also:
setSecure(boolean)
public String
getName()
返回 cookie 的名称。名称在创建之后不得更改。
return |
指定 cookie 名称的 String
|
getName
public String getName()
- Returns the name of the cookie. The name cannot be changed after
creation.
- Returns:
- a
String
specifying the cookie's name
public void
setValue(String newValue)
在创建 cookie 之后将新值分配给 cookie。如果使用二进制值,则可能需要使用 BASE64 编码。
对于 Version 0 cookie,值不应包含空格、方括号、圆括号、等号、逗号、双引号、斜杠、问号、at 符号、冒号和分号。空值在所有浏览器上的行为不一定相同。
newValue |
指定新值的 String |
See also | getValue, javax.servlet.http.Cookie |
setValue
public void setValue(String newValue)
- 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.
- Parameters:
newValue
- aString
specifying the new value- See Also:
getValue()
,Cookie
public String
getValue()
返回 cookie 的值。
return |
包含 cookie 当前值的 String
|
See also | setValue, javax.servlet.http.Cookie |
getValue
public String getValue()
- Returns the value of the cookie.
- Returns:
- a
String
containing the cookie's present value - See Also:
setValue(java.lang.String)
,Cookie
public int
getVersion()
返回此 cookie 遵守的协议版本。版本 1 遵守 RFC 2109,版本 0 遵守根据 Netscape 起草的原始 cookie 规范。浏览器提供的 cookie 使用并标识该浏览器的 cookie 版本。
return | 如果 cookie 遵守原始 Netscape 规范,则返回 0;如果 cookie 遵守 RFC 2109,则返回 1 |
See also | setVersion |
getVersion
public int getVersion()
- Returns the version of the protocol this cookie complies
with. Version 1 complies with RFC 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.
- Returns:
- 0 if the cookie complies with the original Netscape specification; 1 if the cookie complies with RFC 2109
- See Also:
setVersion(int)
public void
setVersion(int v)
设置此 cookie 遵守的 cookie 协议版本。版本 0 遵守原始 Netscape cookie 规范。版本 1 遵守 RFC 2109。
因为 RFC 2109 仍然有点新,所以根据经验可考虑使用版本 1;但在生产网站上不要使用它。
v | 如果 cookie 应该遵守原始 Netscape 规范,则该参数为 0;如果 cookie 应该遵守 RFC 2109,则该参数为 1 |
See also | getVersion |
setVersion
public void setVersion(int v)
- 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 2109.
Since RFC 2109 is still somewhat new, consider version 1 as experimental; do not use it yet on production sites.
- Parameters:
v
- 0 if the cookie should comply with the original Netscape specification; 1 if the cookie should comply with RFC 2109- See Also:
getVersion()
public Object
clone()
重写标准 java.lang.Object.clone
方法以返回此 cookie 的副本。
英文文档:
clone
public Object clone()
- Overrides the standard
java.lang.Object.clone
method to return a copy of this cookie.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Submit a bug or feature
Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!