标签:
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话。常用的会话跟踪技术是Cookie与Session。Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份。
在程序中,会话跟踪是很重要的事情。理论上,一个用户的所有请求操作都应该属于同一个会话,而另一个用户的所有请求操作则应该属于另一个会话,二者不能混淆。例如,用户A在超市购买的任何商品都应该放在A的购物车内,不论是用户A什么时间购买的,这都是属于同一个会话的,不能放入用户B或用户C的购物车内,这不属于同一个会话。
而Web应用程序是使用HTTP协议传输数据的。HTTP协议是无状态的协议。一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接。这就意味着服务器无法从连接上跟踪会话。即用户A购买了一件商品放入购物车内,当再次购买商品时服务器已经无法判断该购买行为是属于用户A的会话还是用户B的会话了。要跟踪该会话,必须引入一种机制。
Cookie就是这样的一种机制。它可以弥补HTTP协议无状态的不足。在Session出现之前,基本上所有的网站都采用Cookie来跟踪会话。
除了name与value之外,Cookie还具有其他几个常用的属性。每个属性对应一个getter方法与一个setter方法。Cookie类的所有属性如表5.1所示。
下面是一个使用所有参数设置Cookie的一个例子。源代码如下:
<%@ page language="java" pageEncoding="UTF-8" %><jsp:directive.page import="java.net.URLEncoder"/><%!boolean isNull(String str){ // 返回字符串是否为空return str==null || str.trim().length()==0;}%><%request.setCharacterEncoding("UTF-8");// 设置request编码if("POST".equals(request.getMethod())){ //如果是POST提交数据String name = request.getParameter("name");// 获取name参数String value = request.getParameter("value");// 获取value参数String maxAge = request.getParameter("maxAge");// 获取maxAge参数String domain = request.getParameter("domain");// 获取domain参数String path = request.getParameter("path");// 获取path参数String comment = request.getParameter("comment");// 获取comment参数String secure = request.getParameter("secure");// 获取secure参数if(!isNull(name)){// 如果name参数不为空Cookie cookie = new Cookie(// 则生成新的CookieURLEncoder.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非空则设置domainif(!isNull(path)) cookie.setPath(path);//若path非空则设置pathif(!isNull(comment)) cookie.setComment(comment);//设置commentif(!isNull(secure)) cookie.setSecure("true".equalsIgnoreCase(secure));response.addCookie(cookie); // 覆盖旧的Cookie}}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Cookie</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><link rel="stylesheet" type="text/css" href="css/style.css"></head><body><div align="center" style="margin:10px; "><fieldset><legend>当前有效的 Cookie</legend><script type="text/javascript">document.write(document.cookie);</script></fieldset><fieldset><legend>设置新 Cookie</legend><form action="setCookie.jsp" method="POST"><table><tr><td>name: </td><td><input name="name" type="text"style="width:200px; "></td></tr><tr><td>value: </td><td><input name="value" type="text"style="width:200px; "></td></tr><tr><td>maxAge: </td><td><input name="maxAge" type="text" style="width:200px; "></td></tr><tr><td>domain: </td><td><input name="domain" type="text"style="width:200px; "></td></tr><tr><td>path: </td><td><input name="path" type="text"style="width:200px; "></td></tr><tr><td>comment: </td><td><input name="comment" type="text"style="width:200px; "></td></tr><tr><td>secure: </td><td><input name="secure" type="text"style="width:200px; "></td></tr><tr><td></td><td><input type="submit" value=" 提 交 " class="button"><input type="button" value=" 刷 新 " onclick="location=‘setCookie.jsp‘"></td></tr></table></form></fieldset></div></body></html>
Cookie的maxAge决定着Cookie的有效期,单位为秒(Second)。Cookie中通过getMaxAge()方法与setMaxAge(int maxAge)方法来读写maxAge属性。
如果maxAge属性为正数,则表示该Cookie会在maxAge秒之后自动失效。浏览器会将maxAge为正数的Cookie持久化,即写到对应的Cookie文件中。无论客户关闭了浏览器还是电脑,只要还在maxAge秒之前,登录网站时该Cookie仍然有效。下面代码中的Cookie信息将永远有效。
Cookie cookie = new Cookie("username", "helloweenvsfei");// 新建Cookiecookie.setMaxAge(Integer.MAX_VALUE);//设置生命周期为MAX_VALUEresponse.addCookie(cookie);// 输出到客户端
如果maxAge为负数,则表示该Cookie仅在本浏览器窗口以及本窗口打开的子窗口内有效,关闭窗口后该Cookie即失效。maxAge为负数的Cookie,为临时性Cookie,不会被持久化,不会被写到Cookie文件中。Cookie信息保存在浏览器内存中,因此关闭浏览器该Cookie就消失了。Cookie默认的maxAge值为-1。
如果maxAge为0,则表示删除该Cookie。Cookie机制没有提供删除Cookie的方法,因此通过设置该Cookie即时失效实现删除Cookie的效果。失效的Cookie会被浏览器从Cookie文件或者内存中删除,例如:
Cookie cookie = new Cookie("username", "helloweenvsfei");// 新建Cookiecookie.setMaxAge(0); //设置生命周期为0,不能为负数response.addCookie(cookie); //必须执行这一句
response对象提供的Cookie操作方法只有一个添加操作add(Cookie cookie)。要想修改Cookie只能使用一个同名的Cookie来覆盖原来的Cookie,达到修改的目的。删除时只需要把maxAge修改为0即可。
注意:
从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交。浏览器提交Cookie时只会提交name与value属性。maxAge属性只被浏览器用来判断Cookie是否过期。
Cookie并不提供修改、删除操作。如果要修改某个Cookie,只需要新建一个同名的Cookie,并添加到response中覆盖原来的Cookie。
如果要删除某个Cookie,只需要新建一个同名的Cookie,并将maxAge设置为0,并添加到response中覆盖原来的Cookie。注意是0而不是负数。负数代表其他的意义。读者可以通过上例的程序进行验证,设置不同的属性。
注意:
修改、删除Cookie时,新建的Cookie除value、maxAge之外的所有属性,例如name、path、domain等,都要与原Cookie完全一样。否则,浏览器将视为两个不同的Cookie不予覆盖,导致修改、删除失败。
标签:
原文地址:http://blog.csdn.net/sunnyyoona/article/details/51893140