java操作cookie小结

Cookie是什么
Cookie是网站为了识别用户身份而储存在用户本地终端(Client Side)上的数据(通常经过加密)。

Cookie 常用来保存少量的、简单的数据,一般不允许超过4KB(与浏览器设置也有关系),使用期限可以进行灵活设定,保存于用户浏览器所设定的目录内。由于信息存储在客户端,最好不要保存敏感数据(例如,密码、银行账号等)。可以保存以下信息: 用户的浏览记录、上次访问时间等内容。同时需要注意客户端用户有可能会关闭 Cookie。

IE浏览器中Cookie的设置
如果您使用的是IE 6.0版本,可以按照以下几个步骤启用/禁用cookie:
(1)点击菜单条上的”工具”(Tool);
(2)在展开的菜单里,选择最下面一条”Internet选项”(Internet Options);
(3)在打开的Internet 选项设置窗口里,顶上有一条标签栏,点击第三个”隐私”(Privacy);
(4)在”隐私”的设置里,中间偏下有三个按钮,点击第二个按钮”高级”(Advanced);
(5)在弹出的cookie设置窗口里,勾选如下设置:
覆盖自动cookie处理
第一方cookie:接受/阻止/提示
第三方cookie:接受/阻止/提示
总是允许会话cookie (Always allow session cookies)
点击按钮”确定”(OK),关闭cookie设置窗口
点击按钮”确定”(OK),关闭Internet 选项设置窗口

IE浏览器中Cookie的查看
如果您使用的是IE 6.0版本,可以按照以下几个步骤启用/禁用cookie:
(1)点击菜单条上的”工具”(Tool);
(2)在展开的菜单里,选择最下面一条”Internet选项”(Internet Options);
(3)在打开的Internet 选项设置窗口里,顶上有一条标签栏,点击第三个”常规”;
(4)在”浏览历史记录”,一栏中点击“设置”;
(5)在打开的新窗口中点击“查看文件”即可。

==========================
java对cookie操作的支持
Servlet API中有一个Cookie类:javax.servlet.http.Cookie 。该类对cookie的使用进行了封装,并包含了一些额外的属性以帮助程序员更好的使用cookie。使用cookie一般分为两个步骤:
(1)创建cookie并设置其值;
(2)读取cookie中所设置的值。
一旦我们以某个域名或者网址的名义(比如:www.darkmi.com)在客户端设置了cookie,那么每次访问该域名或者网址的时候,请求中都会包含该cookie。

Cookie类的常用方法:


public Cookie(java.lang.String name,java.lang.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.


public void setDomain(java.lang.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 not a.b.foo.com). By default, cookies are only returned to the server that sent them.


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.

如果设置为负值的话,则为浏览器进程cookie(内存中保存),关闭浏览器就失效。

如果设置为 0 的话,则该cookie会被删除。本文的最后一个示例演示了如何删除cookie。

更多方法见:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/Cookie.html

==========================
Java操作Cookie之添加Cookie:


//创建Cookie
javax.servlet.http.Cookie nameCookie = new javax.servlet.http.Cookie(“name”, “darkmi”);
javax.servlet.http.Cookie pwdCookie = new javax.servlet.http.Cookie(“password”,”darkmi123″);
javax.servlet.http.Cookie optCookie = new javax.servlet.http.Cookie(“option”,”1982″);

//设置Cookie的生命周期
nameCookie.setMaxAge(60*60*24*365);
pwdCookie.setMaxAge(60*60*24*365);
optCookie.setMaxAge(60*60*24*365);

response.addCookie(nameCookie);
response.addCookie(pwdCookie);
response.addCookie(optCookie);

==========================
Java操作Cookie之读取Cookie:


javax.servlet.http.Cookie[] cookies = request.getCookies();
if (cookies != null) {
String name = “”;
String password = “”;
String option = “”;
javax.servlet.http.Cookie c = null;
for (int i = 0; i < cookies.length; i++) { c = cookies[i]; if (c.getName().equalsIgnoreCase("name")) { name = c.getValue(); } else if (c.getName().equalsIgnoreCase("password")) { password = c.getValue(); } else if (c.getName().equalsIgnoreCase("option")) { option = c.getValue(); } c = null; } System.out.println("name - > ” + name);
System.out.println(“password – > ” + password);
System.out.println(“option – > ” + option);
}

==========================
Java操作Cookie之删除Cookie:


javax.servlet.http.Cookie[] cookies = request.getCookies();
if (cookies != null) {
javax.servlet.http.Cookie c = null;
for (int i = 0; i < cookies.length; i++) { c = cookies[i]; if (c.getName().equalsIgnoreCase("name")) { c.setMaxAge(0); response.addCookie(c); } else if (c.getName().equalsIgnoreCase("password")) { c.setMaxAge(0); response.addCookie(c); } else if (c.getName().equalsIgnoreCase("option")) { c.setMaxAge(0); response.addCookie(c); } c = null; } }

参考链接:
http://www.javamex.com/tutorials/servlets/cookies_api.shtml
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Cookies.html

此条目发表在java/j2ee分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据