1 /** 2 * 读取所有cookie 3 * 注意二、从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交。浏览器提交Cookie时只会提交name与value属性。maxAge属性只被浏览器用 来判断Cookie是否过期 4 * @param request 5 * @param response 6 */ 7 @RequestMapping("/showCookies") 8 public void showCookies(HttpServletRequest request,HttpServletResponse response ){ 9 10 Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组 11 if (null==cookies) { 12 System.out.println("没有cookie========="); 13 } else { 14 for(Cookie cookie : cookies){ 15 System.out.println("name:"+cookie.getName()+",value:"+ cookie.getValue()); 16 } 17 } 18 19 } 20 /** 21 * 添加cookie 22 * @param response 23 * @param name 24 * @param value 25 */ 26 @RequestMapping("/addCookie") 27 public void addCookie(HttpServletResponse response,String name,String value){ 28 Cookie cookie = new Cookie(name.trim(), value.trim()); 29 cookie.setMaxAge(30 * 60);// 设置为30min 30 cookie.setPath("/"); 31 System.out.println("已添加==============="); 32 response.addCookie(cookie); 33 } 34 /** 35 * 修改cookie 36 * @param request 37 * @param response 38 * @param name 39 * @param value 40 * 注意一、修改、删除Cookie时,新建的Cookie除value、maxAge之外的所有属性,例如name、path、domain等,都要与原Cookie完全一样。否则,浏览器将视为两个不 同的Cookie不予覆盖,导致修改、删除失败。 41 */ 42 @RequestMapping("/editCookie") 43 public void editCookie(HttpServletRequest request,HttpServletResponse response,String name,String value){ 44 Cookie[] cookies = request.getCookies(); 45 if (null==cookies) { 46 System.out.println("没有cookie=============="); 47 } else { 48 for(Cookie cookie : cookies){ 49 if(cookie.getName().equals(name)){ 50 System.out.println("原值为:"+cookie.getValue()); 51 cookie.setValue(value); 52 cookie.setPath("/"); 53 cookie.setMaxAge(30 * 60);// 设置为30min 54 System.out.println("被修改的cookie名字为:"+cookie.getName()+",新值为:"+cookie.getValue()); 55 response.addCookie(cookie); 56 break; 57 } 58 } 59 } 60 61 } 62 /** 63 * 删除cookie 64 * @param request 65 * @param response 66 * @param name 67 */ 68 @RequestMapping("/delCookie") 69 public void delCookie(HttpServletRequest request,HttpServletResponse response,String name){ 70 Cookie[] cookies = request.getCookies(); 71 if (null==cookies) { 72 System.out.println("没有cookie=============="); 73 } else { 74 for(Cookie cookie : cookies){ 75 if(cookie.getName().equals(name)){ 76 cookie.setValue(null); 77 cookie.setMaxAge(0);// 立即销毁cookie 78 cookie.setPath("/"); 79 System.out.println("被删除的cookie名字为:"+cookie.getName()); 80 response.addCookie(cookie); 81 break; 82 } 83 } 84 } 85 } 86 87 /** 88 * 根据名字获取cookie 89 * @param request 90 * @param name cookie名字 91 * @return 92 */ 93 public Cookie getCookieByName(HttpServletRequest request,String name){ 94 Map<String,Cookie> cookieMap = ReadCookieMap(request); 95 if(cookieMap.containsKey(name)){ 96 Cookie cookie = (Cookie)cookieMap.get(name); 97 return cookie; 98 }else{ 99 return null; 100 } 101 } 102 103 104 105 /** 106 * 将cookie封装到Map里面 107 * @param request 108 * @return 109 */ 110 private Map<String,Cookie> ReadCookieMap(HttpServletRequest request){ 111 Map<String,Cookie> cookieMap = new HashMap<String,Cookie>(); 112 Cookie[] cookies = request.getCookies(); 113 if(null!=cookies){ 114 for(Cookie cookie : cookies){ 115 cookieMap.put(cookie.getName(), cookie); 116 } 117 } 118 return cookieMap; 119 } 120