javax.servlet.http.Cookie翻译

简介:
没啥好说的,Servlet规范里的东西,英文看着不爽就翻译了。有不恰当的地方,还望高手指正。
 
 

javax.servlet.http 
Class Cookie

java.lang.Object
  |
  +--javax.servlet.http.Cookie

public class  Cookie
extends java.lang.Object
implements java.lang.Cloneable
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. 创建一个cookie,一个小量信息通过Servlet发送给Web浏览器,通过浏览器保存,以后再从浏览器发送给服务器。一个cookie的值能唯一识别一个客户端,因此cookie常用于session的管理。
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. 一个cookie有一个名字和一个值和一些可选择的属性例如注释、路径、域、最大有效时间、版本号等。一些浏览器在处理可选项时有bug,因此应保守使用以提高sevlet的交互性。
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. Sevlet发送cookie使用 HttpServletResponse.addCookie(javax.servlet.http.Cookie) 方法,一个浏览器预计最多可支持300个cookie,对一个站点最多支持20个cookie,并且可能限制一个cookie的大小不超过4KB。
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. 浏览器通过HTTP请求的处理者返回cookie到一个servlet。cookie能被从 HttpServletRequest.getCookies() 中重新获取到。
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. Cookie影响每一个使用它的web页。 (......)
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. 这个类同时支持版本0和版本1的cookie规范。默认情况下,cookie使用版本0来创建以确保最好的交互性。

Constructor Summary
Cookie(java.lang.String name, java.lang.String value) 
          Constructs a cookie with a specified name and value. 使用指定的名称和值构建一个cookie。
 
Method Summary
 java.lang.Object clone() 
          Overrides the standard java.lang.Object.clone method to return a copy of this cookie. 返回cookie克隆的副本,这个方法覆盖了java.lang.Object.clone()方法。
 java.lang.String getComment() 
          Returns the comment describing the purpose of this cookie, or null if the cookie has no comment.返回一个描述cookie用途的注释,如果没有注释,则返回null。
 java.lang.String getDomain() 
          Returns the domain name set for this cookie.返回一个cookie域名。(域名指定cookie在哪个域中有效,例如:Domain=.blog.51cto.com)
 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将一直有效直到浏览器关闭。
 java.lang.String getName() 
          Returns the name of the cookie.返回cookie的名字。
 java.lang.String getPath() 
          Returns the path on the server to which the browser returns this cookie.返回cookie对服务器上哪个url有效。
 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。
 java.lang.String getValue() 
          Returns the value of the cookie. 返回cookie的值。
 int getVersion() 
          Returns the version of the protocol this cookie complies with.返回cookie内容所遵循的版本。(目前只有值为1时可用)。
 void setComment(java.lang.String purpose) 
          Specifies a comment that describes a cookie's purpose.设置描述cookie用途的注释。
 void setDomain(java.lang.String pattern) 
          Specifies the domain within which this cookie should be presented.设置cookie在哪一个域中使用。
 void setMaxAge(int expiry) 
          Sets the maximum age of the cookie in seconds.设置cookie最大的有效时间。
 void setPath(java.lang.String uri) 
          Specifies a path for the cookie to which the client should return the cookie.设置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.设置是否使用安全协议如HTTPS或SSL等发送cookie。
 void setValue(java.lang.String newValue) 
          Assigns a new value to a cookie after the cookie is created. 给一个cookie重新指定一个新值。
 void setVersion(int v) 
          Sets the version of the cookie protocol this cookie complies with.设置cookie使用的协议版本。
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Constructor Detail

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)
Method Detail

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:
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:
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:
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:
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:
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



本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/78163,如需转载请自行联系原作者
相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
5天前
Servlet 教程 之 Servlet 服务器 HTTP 响应 2
Servlet教程讲解了如何通过HttpServletResponse设置HTTP响应,包括编码URL、添加cookie、设置报头、控制缓冲区、发送错误或重定向响应。方法如encodeURL、addCookie、sendError、sendRedirect等,涉及状态码、报头、字符编码和内容长度的管理。
16 2
|
6天前
|
XML Java 数据格式
Servlet 教程 之 Servlet 客户端 HTTP 请求 3
该教程展示了如何在Servlet中处理客户端HTTP请求,特别是获取HTTP头信息。示例代码创建了一个名为`DisplayHeader`的Servlet,它扩展了`HttpServlet`并重写了`doGet`方法。在`doGet`中,使用`HttpServletRequest`的`getHeaderNames()`遍历所有头部,显示其名称和对应值。Servlet在TomcatTest项目下,通过`web.xml`配置映射到`/TomcatTest/DisplayHeader`路径。
28 14
|
6天前
|
安全 Java 网络安全
Servlet 教程 之 Servlet 客户端 HTTP 请求 2
Servlet教程介绍了如何在Servlet中处理HTTP请求,包括获取Cookie、头信息、参数、Session等。方法如:`getCookies()`、`getAttributeNames()`、`getHeaderNames()`、`getParameterNames()`等。还能获取身份验证类型、字符编码、MIME类型、请求方法、远程用户信息、URL路径、安全通道状态以及请求内容长度等。此外,可通过`getSession()`创建或获取Session,并以`Map`形式获取参数。
20 8
|
5天前
|
XML Java 数据格式
Servlet 教程 之 Servlet 服务器 HTTP 响应 3
`Servlet`教程示例展示了如何创建一个HTTP响应,使用`@WebServlet("/Refresh")`的`Refresh`类继承`HttpServlet`。在`doGet`方法中,设置了`Refresh`头以每5秒自动刷新,并用`setContentType("text/html;charset=UTF-8")`设定内容类型。还使用`Calendar`和`SimpleDateFormat`获取并格式化当前时间显示。相应的`web.xml`配置指定了Servlet路径。当访问此Servlet时,页面将每5秒更新一次显示的系统时间。
16 4
|
5天前
|
数据安全/隐私保护
Servlet 教程 之 Servlet HTTP 状态码 1
Servlet教程讲解了HTTP状态码,如200(成功)、404(未找到)和500(服务器错误)。状态码帮助标识HTTP响应的状态,包括继续请求、重定向、权限问题、方法不允许和服务器故障等不同情况。这些代码是通信中的关键反馈元素。
12 3
|
4天前
|
Java
Servlet 教程 之 Servlet HTTP 状态码 3
该Servlet教程聚焦于HTTP状态码,示例展示如何向客户端发送407错误,提示"Need authentication!!!". 类名为`showError`的Servlet扩展自`HttpServlet`,重写`doGet`和`doPost`方法。当遇到GET或POST请求时,它会设置HTTP状态码为407并附带错误信息。
11 2
|
4天前
Servlet 教程 之 Servlet HTTP 状态码 2
Servlet教程讲解了设置HTTP状态码的方法:1)setStatus(int statusCode)用于设定任意状态码,需在返回内容前调用;2)sendRedirect(String url)生成302响应,重定向至新URL;3)sendError(int code, String message)发送错误状态码(如404)并附带自动格式化的错误信息。
10 0
|
6天前
|
存储 缓存
Servlet 教程 之 Servlet 服务器 HTTP 响应 1
Servlet服务器的HTTP响应包括状态行(如HTTP/1.1 200 OK)、响应头部(如Content-Type, Cache-Control)和HTML文档。关键头部有:Allow指示支持的请求方法;Cache-Control定义缓存策略;Content-Type指定MIME类型;Expires和Last-Modified管理缓存;Location用于重定向;Set-Cookie用于设置cookie。
21 8
|
7天前
Servlet 教程 之 Servlet 客户端 HTTP 请求 1
Servlet教程讲解了客户端HTTP请求,包括各种重要头信息:Accept(定义可处理的MIME类型)、Accept-Charset(指定字符集)、Accept-Encoding(处理的编码类型)、Accept-Language(首选语言)、Authorization(身份验证)、Connection(是否支持持久连接)、Content-Length(POST数据大小)、Cookie(返回cookies)、Host(指定URL的主机和端口)、If-Modified-Since(检查页面更新)、If-Unmodified-Since(条件性操作)。
9 0
|
7月前
|
XML 开发框架 Java
Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)
Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)