Cookie
public Cookie(java.lang.String name,
java.lang.String value)
- Constructs a cookie with a specified name and value. 使用指定的名称和值创建一个cookie。
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. 这个名称必须遵循RFC 2109的规范。这意味着它仅仅能包含ASCII码字符,并且不能包含逗号,分号,或者空格,或者以$开头的字符串,cookie的名字在创建后就不能再改变了。
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. 这个值可以发送给任何的服务器,但是仅对某个服务器有作用。这个值在创建后可以通过
setValue
方法来改变。
By default, cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion
method. 默认情况下,依照Netscape cookie规范来创建,版本可以通过setVersion
来改变。
- Parameters:
-
name
- a String
specifying the name of the cookie 一个指定cookie名字的字符串。
-
value
- a String
specifying the value of the cookie 一个指定cookie值的字符串。
- Throws:
- java.lang.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)
setComment
public void setComment(java.lang.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. 设置用来描述cookie目的的注释,如果浏览器将这个cookie给一个使用者,这个信息将很有用。注释不支持通过Netscape版本0的cookie。
- Parameters:
-
purpose
- a String
specifying the comment to display to the user 一个显示给使用者的字符串注释。
- See Also:
- getComment()
getComment
public java.lang.String getComment()
- Returns the comment describing the purpose of this cookie, or
null
if the cookie has no comment.返回一个描述cookie意图的注释,当没有注释的时候返回null。
- Returns:
- a
String
containing the comment, or null
if none
- See Also:
- setComment(java.lang.String)
setDomain
public void setDomain(java.lang.String pattern)
- Specifies the domain within which this cookie should be presented. 指定cookie将在哪个域里面有效。
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, [url]www.foo.com[/url]
, but not a.b.foo.com
). By default, cookies are only returned to the server that sent them. 如果域名使用RFC 2109规范,那么名字应该以点开始 (.foo.com
),并且意味着这个cookie在指定的域名系统(DNS)下是有效的,比如([url]www.foo.com[/url]
,但不是a.b.foo.com
)。默认情况下,cookie仅能返回给发送他们的服务器。
- Parameters:
-
pattern
- a String
containing the domain name within which this cookie is visible; form is according to RFC 2109
- See Also:
- getDomain()
getDomain
public java.lang.String getDomain()
- Returns the domain name set for this cookie. The form of the domain name is set by RFC 2109.返回一个cookie域名。域名使用RFC 2109规范。
- Returns:
- a
String
containing the domain name
- See Also:
- setDomain(java.lang.String)
setMaxAge
public void setMaxAge(int expiry)
- Sets the maximum age of the cookie in seconds. 以秒为单位设置cookie最大的有效时间。
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. 一个明确值指定cookie将在多少秒过后失效。注意,这个值是cookie有效期满的最大值,不是当前cookie的存在时间。
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.一个负值意味着cookie不连续存储,并且在浏览器推出的时候删除。一个0值将导致cookie被删除。
- 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()
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. 返回cookie最大有效期,默认情况下,-1表示cookie将一直有效,直到浏览器关闭。
- 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)
setPath
public void setPath(java.lang.String uri)
- Specifies a path for the cookie to which the client should return the cookie. 为应该返回cookie的客户端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. 这个cookie将对指定目录中及其子目录所有的页可见。一个cookie路径必须包含设置它的那个servlet,例如,
/catalog,这使得cookie在
/catalog下可见。
Consult RFC 2109 (available on the Internet) for more information on setting path names for cookies.查看RFC 2109规范,有更多关于设置路径名的信息。
- Parameters:
-
uri
- a String
specifying a path
- See Also:
- getPath()
getPath
public java.lang.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.返回一个cookie返回服务器的路径,并且这个cookie对服务器上该路径下所有的子路径有效。
- Returns:
- a
String
specifying a path that contains a servlet name, for example, /catalog
- See Also:
- setPath(java.lang.String)
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. 指定浏览器将使用安全协议(例如,HTTPS 或 SSL)发送cookie。
The default value is false
. 默认值是false。
- Parameters:
-
flag
- if true
, sends the cookie from the browser to the server only when using a secure protocol; if false
, sent on any protocol
- See Also:
- getSecure()
getSecure
public 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. 如果浏览器仅仅安全协议下发送cookie则返回true。浏览器能使用任何协议发送cookie则返回fasle。
- Returns:
-
true
if the browser uses a secure protocol; otherwise, true
- See Also:
- setSecure(boolean)
getName
public java.lang.String getName()
- Returns the name of the cookie. The name cannot be changed after creation. 返回cookie的名字。在创建后名字不能改变。
- Returns:
- a
String
specifying the cookie's name
setValue
public void setValue(java.lang.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. 为一个已经创建好的cookie指定一个新值,如果使用二进制值,则可能要使用BASE64编码。
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. 在版本0cookie里,不能包含空格、括号、圆括号、等号、逗号、双引号、问号、冒号等等。空值一样可以在所有浏览器上工作。
- Parameters:
-
newValue
- a String
specifying the new value
- See Also:
-
getValue(), Cookie
getValue
public java.lang.String getValue()
- Returns the value of the cookie.返回一个cookie的值。
- Returns:
- a
String
containing the cookie's present value
- See Also:
-
setValue(java.lang.String), Cookie
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. 返回cookie遵循的版本。版本1遵循RFC 2109规范,版本0遵循老的Netscape制定的cookie规范。cookie只能通过浏览器使用来标识,并且认出一个cookie的版本。
- Returns:
- 0 if the cookie complies with the original Netscape specification; 1 if the cookie complies with RFC 2109
- See Also:
- setVersion(int)
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. 设置cookie所遵循的本版。版本0遵循Netscape公司的cookie规范,版本1遵循RFC 2109规范。
Since RFC 2109 is still somewhat new, consider version 1 as experimental; do not use it yet on production sites.由于RFC 2109是较新的规范,认为版本1是一个实验规范:不能适用于生产环境。
- 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()
clone
public java.lang.Object clone()
- Overrides the standard
java.lang.Object.clone
method to return a copy of this cookie. 返回cookie克隆的副本,这个方法覆盖了java.lang.Object.clone
()方法。
- Overrides:
- clone in class java.lang.Object