您的位置:首页 > 编程语言 > Java开发

[Java开发之路](26)Cookie机制

2016-07-12 22:28 281 查看

[b]1.Cookie机制
[/b]

会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话。常用的会话跟踪技术是Cookie与Session。Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份。

在程序中,会话跟踪是很重要的事情。理论上,一个用户的所有请求操作都应该属于同一个会话,而另一个用户的所有请求操作则应该属于另一个会话,二者不能混淆。例如,用户A在超市购买的任何商品都应该放在A的购物车内,不论是用户A什么时间购买的,这都是属于同一个会话的,不能放入用户B或用户C的购物车内,这不属于同一个会话。

而Web应用程序是使用HTTP协议传输数据的。HTTP协议是无状态的协议。一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接。这就意味着服务器无法从连接上跟踪会话。即用户A购买了一件商品放入购物车内,当再次购买商品时服务器已经无法判断该购买行为是属于用户A的会话还是用户B的会话了。要跟踪该会话,必须引入一种机制。

Cookie就是这样的一种机制。它可以弥补HTTP协议无状态的不足。在Session出现之前,基本上所有的网站都采用Cookie来跟踪会话。

[b]2.设置Cookie的所有属性
[/b]

除了name与value之外,Cookie还具有其他几个常用的属性。每个属性对应一个getter方法与一个setter方法。Cookie类的所有属性如表5.1所示。



下面是一个使用所有参数设置Cookie的一个例子。源代码如下:

<%@pagelanguage="java"pageEncoding="UTF-8"%>

<jsp:directive.pageimport="java.net.URLEncoder"/>

<%!

booleanisNull(Stringstr){//返回字符串是否为空

returnstr==null||str.trim().length()==0;

}

%>

<%

request.setCharacterEncoding("UTF-8");

//设置request编码


if("POST".equals(request.getMethod())){//

如果是POST提交数据

Stringname=request.getParameter("name");

//获取name参数

Stringvalue=request.getParameter("value");

//获取value参数

StringmaxAge=request.getParameter("maxAge");

//获取maxAge参数

Stringdomain=request.getParameter("domain");

//获取domain参数

Stringpath=request.getParameter("path");

//获取path参数

Stringcomment=request.getParameter("comment");

//获取comment参数

Stringsecure=request.getParameter("secure");

//获取secure参数


if(!isNull(name)){

//如果name参数不为空

Cookiecookie=newCookie(

//则生成新的Cookie

URLEncoder.encode(name,"UTF-8"),

URLEncoder.encode(value,"UTF-8"));


//若maxAge非空则设置maxAge属性

if(!isNull(maxAge))cookie.setMaxAge(Integer.

parseInt(maxAge));

if(!isNull(domain))cookie.setDomain(domain);

//

若domain非空则设置domain

if(!isNull(path))cookie.setPath(path);

//

若path非空则设置path

if(!isNull(comment))cookie.setComment(comment);

//

设置comment

if(!isNull(secure))cookie.setSecure

("true".equalsIgnoreCase(secure));


response.addCookie(cookie);//覆盖旧的Cookie

}

}

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<title>Cookie</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

<linkrel="stylesheet"type="text/css"href="css/style.css">

</head>

<body>

<divalign="center"style="margin:10px;">

<fieldset>

<legend>当前有效的Cookie</legend>

<scripttype="text/javascript">

document.write(document.cookie);

</script>

</fieldset>

<fieldset>

<legend>设置新Cookie</legend>

<formaction="setCookie.jsp"method="POST">

<table>

<tr><td>name:</td>

<td><inputname="name"type="text"

style="width:200px;">

</td>

</tr>

<tr><td>value:</td>

<td><inputname="value"type="text"

style="width:200px;">

</td>

</tr>

<tr><td>maxAge:</td>

<td><inputname="maxAge"type="

text"style="width:

200px;"></td>

</tr>

<tr><td>domain:</td>

<td><inputname="domain"type="text"

style="width:

200px;"></td>

</tr>

<tr><td>path:</td>

<td><inputname="path"type="text"

style="width:200px;">

</td>

</tr>

<tr><td>comment:</td>

<td><inputname="comment"type="text"

style="width:

200px;"></td>

</tr>

<tr><td>secure:</td>

<td><inputname="secure"type="text"

style="width:

200px;"></td>

</tr>

<tr><td></td>

<td><inputtype="submit"value="提交"class=

"button">

<inputtype="button"value="刷新"onclick=

"location='setCookie.jsp'">

</td>

</tr>

</table>

</form>

</fieldset>

</div>

</body>

</html>



3.Cookie的有效期


Cookie的maxAge决定着Cookie的有效期,单位为(Second)。Cookie中通过getMaxAge()方法与setMaxAge(intmaxAge)方法来读写maxAge属性。

如果maxAge属性为正数,则表示该Cookie会在maxAge秒之后自动失效。浏览器会将maxAge为正数的Cookie持久化,即写到对应的Cookie文件中。无论客户关闭了浏览器还是电脑,只要还在maxAge秒之前,登录网站时该Cookie仍然有效。下面代码中的Cookie信息将永远有效。

Cookiecookie=newCookie("username","helloweenvsfei");

//新建Cookie

cookie.setMaxAge(Integer.MAX_VALUE);

//设置生命周期为MAX_VALUE

response.addCookie(cookie);

//输出到客户端


如果maxAge为负数,则表示该Cookie仅在本浏览器窗口以及本窗口打开的子窗口内有效,关闭窗口后该Cookie即失效。maxAge为负数的Cookie,为临时性Cookie,不会被持久化,不会被写到Cookie文件中。Cookie信息保存在浏览器内存中,因此关闭浏览器该Cookie就消失了。Cookie默认的maxAge值为-1。

如果maxAge为0,则表示删除该Cookie。Cookie机制没有提供删除Cookie的方法,因此通过设置该Cookie即时失效实现删除Cookie的效果。失效的Cookie会被浏览器从Cookie文件或者内存中删除,例如:

Cookiecookie=newCookie("username","helloweenvsfei");

//新建Cookie

cookie.setMaxAge(0);//

设置生命周期为0,不能为负数

response.addCookie(cookie);//

必须执行这一句


response对象提供的Cookie操作方法只有一个添加操作add(Cookiecookie)。要想修改Cookie只能使用一个同名的Cookie来覆盖原来的Cookie,达到修改的目的。删除时只需要把maxAge修改为0即可。

注意:

从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交。浏览器提交Cookie时只会提交name与value属性。maxAge属性只被浏览器用来判断Cookie是否过期。

[b]4.Cookie的修改与删除
[/b]

Cookie并不提供修改、删除操作。如果要修改某个Cookie,只需要新建一个同名的Cookie,并添加到response中覆盖原来的Cookie。

如果要删除某个Cookie,只需要新建一个同名的Cookie,并将maxAge设置为0,并添加到response中覆盖原来的Cookie。注意是0而不是负数。负数代表其他的意义。读者可以通过上例的程序进行验证,设置不同的属性。

注意:

修改、删除Cookie时,新建的Cookie除value、maxAge之外的所有属性,例如name、path、domain等,都要与原Cookie完全一样。否则,浏览器将视为两个不同的Cookie不予覆盖,导致修改、删除失败。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: