展会信息港展会大全

java中cookie操作详细
来源:互联网   发布日期:2016-03-01 10:52:49   浏览:1709次  

导读:1.设置Cookie 代码如下 Cookie cookie = new Cookie(key, value); cookie.setMaxAge(60); 设置60秒生存期,如果设置为负值的话,则为浏览器进程Cookie(内存中保存),关闭浏览器就失效。 代码如下 cookie.setPath(/test/test2); 设置Cookie路径,不设置的话为...

1.设置Cookie

代码如下

Cookie cookie = new Cookie("key", "value");

cookie.setMaxAge(60);

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

代码如下

cookie.setPath("/test/test2");

设置Cookie路径,不设置的话为当前路径(对于Servlet来说为request.getContextPath() + web.xml里配置的该Servlet的url-pattern路径部分) 。

代码如下

response.addCookie(cookie);

2.读取Cookie

该方法可以读取当前路径以及“直接父路径”的所有Cookie对象,如果没有任何Cookie的话,则返回null。

代码如下

Cookie[] cookies = request.getCookies();

3.删除Cookie

代码如下

Cookie cookie = new Cookie("key", null);

cookie.setMaxAge(0);

设置为0为立即删除该Cookie;

代码如下

cookie.setPath("/test/test2");

删除指定路径上的Cookie,不设置该路径,默认为删除当前路径Cookie;

代码如下 response.addCookie(cookie);

下面用一个完整的实例来说明

代码如下

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.3lian.com/">

<html xmlns="http://www.3lian.com/">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

</head>

<body>

<%

String username = null;

Cookie[] cookies = request.getCookies();

if(cookies!=null)

{

for(int i=0;i<cookies.length;i++)

{

if("cookies_user".equals(cookies[i].getName()))

{

username = cookies[i].getValue();//cookies_user}

}

if("onepc".equals(username))

{

out.println("Hello");

}else

{

%>

<table width="302" border="1">

<form id="form1" name="form1" method="post" action="clogin.jsp">

<tr>

<td width="79"><div align="center"></div></td>

<td width="207"><input type="text" name="user" id="user" /></td>

</tr>

<tr>

<td><div align="center"></div></td>

<td><input type="text" name="textfield2" id="textfield2" /></td>

</tr>

<tr>

<td><div align="center">

</div></td>

<td><select name="select" id="select">

<option value="31536000">one year</option>

<option value="120">two min</option>

</select></td>

</tr>

<tr>

<td colspan="2"><label>

<input type="submit" name="button" id="button" value="" />

</label></td>

</tr>

</form>

</table>

<%

}

}

%>

</body>

</html>

login.jsp

代码如下

<%

String user = request.getParameter("user");

Cookie cookie = new Cookie("cookies_user",user);

cookie.setMaxAge(120);

response.addCookie(cookie);

response.sendRedirect("cindex.jsp");

%>

4.注意:假设路径结构如下

代码如下

test/test2/test345/test555/test666

a.相同键名的Cookie(值可以相同或不同)可以存在于不同的路径下。

b. 删除时,如果当前路径下没有键为"key"的Cookie,则查询全部父路径,检索到就执行删除操作(每次只能删除一个与自己最近的父路径Cookie) FF.必须指定与设定cookie时使用的相同路径来删除改cookie,而且cookie的键名不论大写、小写或大小混合都要指定路径。IE.键名小写时,如果当前路径为/test/test2,如果找不到再向上查询/test、/test555、/test345,如果还找不到就查询/(/test555/test666不查询) 。键名大小写混合或大写时,不指定路径则默认删除当前路径,并且不向上查询。

c.读取Cookie时只能读取直接父路径的Cookie。如果当前路径为/test/test2,要读取的键为“key”。当前路径读取后,还要读取/test,/test读取后,还要读取/ 。

d.在做Java的web项目时,由于一般的Web服务器(如Tomcat或Jetty)都用Context来管理不同的Web Application,这样对于每个Context有不同的Path,在一个Server中有多个Web Application时要特别小心,不要设置Path为/的Cookie,容易误操作(当然前提是域名相同) 。

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港