一、Cookie对象1. 将只需在客户端保存或处理的数据放在本地计算机提高网络传输速度2. 服务器端发送Cookie对象到客户端并将Cookie对象保存到客户端下次再访问服务器时客户端再将Cookie对象作为通行证带回服务器端3. 属于浏览器技术只要Cookie对象不失效无论服务器是否关闭都会一直存在应用七天账号密码免登录二、Cookie对象的创建、发送、获取1. 创建Cookie对象 Cookie cookienew Cookie(String name,String value);2. 发送Cookie对象到浏览器 resp.addCookie(cookie);3. 获取Cookie对象数组遍历 Cookie[] cookies req.getCookies()/* 1. 创建Cookie对象 */ Cookie cookie01 new Cookie(cook01,cook01); /* 2. 发送Cookie对象到浏览器 */ resp.addCookie(cookie01); /* 3. 获取Cookie对象 */ //3.1 获取Cookie对象数组 Cookie[] cookies req.getCookies(); //3.2 遍历Cookie对象数组 if(cookies ! null cookies.length 0) { for(Cookie cookie : cookies) { String name cookie.getName(); String value cookie.getValue(); System.out.println(名称name,值value); } }三、Cookie对象的到期时间方法setMaxAge(int time)1. 负整数(默认)默认-1将Cookie对象只保存在浏览器关闭浏览器就失效2. 正整数保存秒数将Cookie对象保存在计算机在计算机后存活对应的秒数后失效3. 零删除Cookie对象Cookie cookie02 new Cookie(cook02, cook02); cookie02.setMaxAge(-1); resp.addCookie(cookie02);四、Cookie注意事项1. 不支持中文若有中文 在Cookie对象创建前需要先对对应的字符串进行编码在获取Cookie对象时再对对应的字符串进行解码2. 只在当前浏览器和当前计算机有效不跨浏览器和计算机3. Cookie对象的name若相同新value覆盖旧value4. Cookie存储数量有上限不同浏览器上限不同//1. 创建Cookie对象前对对应的字符串进行编码 String name URLEncoder.encode(姓名); String value URLEncoder.encode(张三); //2. 创建Cookie对象 Cookie cookie05 new Cookie(name, value); //3. 发送Cookie对象 resp.addCookie(cookie05); //4. 获取Cookie对象 Cookie[] cookies req.getCookies(); if (cookies ! null cookies.length 0) { for (Cookie cookie : cookies) { //5. 获取时对对应的字符串进行解码 String nameC URLDecoder.decode(cookie.getName()); String valueC URLDecoder.decode(cookie.getValue()); System.out.println(name:nameC,value:valueC); } }五、Cookie对象的路径当前服务器下,setPath(路径)1. 任何项目的任何资源都可获取Cookie对象 //setPath(/)2. 当前项目下的资源可获取Cookie对象 //setPath(/当前站点名)或者默认3. 指定项目下的资源可获取Cookie对象 //setPath(/指定站点名)4. 指定的资源可获取Cookie对象 //setPath(/站点名/指定资源)/* 1. 任何项目的任何资源都可获取Cookie对象 */ Cookie cookie06 new Cookie(cook06, cook06); cookie06.setPath(/); resp.addCookie(cookie06); /* 2. 当前项目下的资源可获取Cookie对象 */ Cookie cookie07 new Cookie(cook07, cook07); cookie07.setPath(/servlet04); resp.addCookie(cookie07); /* 3. 指定项目下的资源可获取Cookie对象 */ Cookie cookie08 new Cookie(cook08, cook08); cookie08.setPath(/servlet03); resp.addCookie(cookie08); /* 4. 指定资源可获取Cookie对象 */ Cookie cookie09 new Cookie(cook09, cook09); cookie09.setPath(/servlet03/ser01); resp.addCookie(cookie09);