cookie & session

来源:互联网 发布:如何用python开发软件 编辑:程序博客网 时间:2024/06/05 01:04

Cookie

基本操作

该数据在HTTP协议中, 往返存在于 HTTP头中

服务器生成cookie时, 在 response 头里面设置方法如下

Set-Cookie: id1=9527; Expires=Thu, 10-Apr-2014 06:22:10 GMTSet-Cookie: id2=4399Set-Cookie: cn=%E4%B8%AD%E6%96%87

其中  Expires 参数值表示这个cookie值的到期时间

浏览器在收到这种头后, 下次发送 request时, 在HTTP头上带上:
Cookie: id=4399; id2=4399; cn=%E4%B8%AD%E6%96%87

servlet中具体操作流程:

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html; charset=UTF-8");// System.out.println(request.getCookies().length);for (Cookie cookie : request.getCookies()) {response.getWriter().print(cookie.getName() + " : "+ URLDecoder.decode(cookie.getValue(), "UTF-8"));response.getWriter().print("\n");/* * 浏览器输出 id : 4399 id2 : 4399 cn : 中文 */}Cookie cookie1 = new Cookie("id1", "9527");Cookie cookie2 = new Cookie("id2", "4399");Cookie cookie3 = new Cookie("cn", URLEncoder.encode("中文", "UTF-8"));//COOKIE 中文需要编码cookie1.setMaxAge(5); // 设置过期时间为 5 秒后response.addCookie(cookie1);response.addCookie(cookie2);response.addCookie(cookie3);}

cookie目录区别

cookie可以设置不同的目录来区别, 浏览器在得到 带路径的cookie后, 只有下次访问对应的资源路径时才在 request中带上cookie值

服务器 response 带路径cookie时
Set-Cookie:id1=9527; Expires=Thu, 10-Apr-2014 07:23:15 GMT; Path=/123Set-Cookie:id2=4399; Path=/123Set-Cookie:cn=%E4%B8%AD%E6%96%87; Path=/123

浏览器在下次访问 /主机名/123 这个资源时才会在request HTTP 头中带上上次收到的 cookie值
Path=/ 时, 访问当前主机所有资源时都会带上该cookie, 默认 cookiepath为 null


public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html; charset=UTF-8");// System.out.println(request.getCookies().length);Cookie cookie1 = new Cookie("id1", "9527");Cookie cookie2 = new Cookie("id2", "4399");Cookie cookie3 = new Cookie("cn", URLEncoder.encode("中文", "UTF-8"));//COOKIE 中文需要编码cookie1.setMaxAge(5); // 设置过期时间为 5 秒后System.out.println(cookie1.getPath()); // 默认为NULLcookie1.setPath("/123");cookie2.setPath("/123");cookie3.setPath("/");System.out.println(cookie1.getPath()); // 默认为NULLresponse.addCookie(cookie1);response.addCookie(cookie2);response.addCookie(cookie3);}

TestCookiePath 为例外一个路径的资源
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.getWriter().print(request.getCookies()[0].getValue()); // 只能得到上个代码片设置的中文cookie}

session

基于cookie中的 JSESSIONID或者 请求URL中的 参数JSESSIONID 值作为 主键
浏览器只保存 主键数据, 实际存储数据在服务器, 服务器缺省 将30分钟未被访问过的session删除
不同于cookie中存储数据格式和大小有限制, session可直接将对象进行缓存

基础操作


服务器从request中获取 session主键时, 诺没有则新建一个, 并在response中下发主键到浏览器
浏览器下次访问服务器时, HTTP中

Cookie: JSESSIONID=9020E6ABEA6D3399822ED2184BCA45B2

servlet中操作过程:

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession seesion = request.getSession(); // 没有则立即创建System.out.println(seesion.isNew());// 此session是否当次交互创建System.out.println(seesion.getAttribute("haa")); // 第二次运行时输出 2312312seesion.setAttribute("haa", 2312312);}


不使用cookie存储主键方法

服务器在处理某些跳转时, 需要使用 session的, 可以在服务器生成跳转URL , 自行将 URL编码成 带 session主键的
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession seesion = request.getSession(); // 没有则立即创建System.out.println(seesion.isNew());// 此session是否当次交互创建System.out.println(seesion.getAttribute("haa")); // 第二次运行时输出 2312312seesion.setAttribute("haa", 2312312);String urlString = "/ServletContext/TestSession2";urlString = response.encodeUrl(urlString);System.out.println(urlString);// 编码为// /ServletContext/TestSession2;jsessionid=8FD1E71653F4FC7F4E0A16ABF62E6870response.sendRedirect(urlString);}













 
0 0
原创粉丝点击