JavaWeb:实现对cookie的增删改查

copy

  1. [java] view plain
  2. /** 
  3.          * 读取所有cookie 
  4.          * 注意二、从客户端读取Cookie时,包括maxAge在内的其他属性都是不可读的,也不会被提交。
  5.  浏览器提交Cookie时只会提交name与value属性。maxAge属性只被浏览器用来判断Cookie是否过期 
  6.          * @param request 
  7.          * @param response 
  8.          */  
  9.         @RequestMapping("/showCookies")  
  10.         public void showCookies(HttpServletRequest request,HttpServletResponse response ){  
  11.                
  12.             Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组  
  13.             if (null==cookies) {  
  14.                 System.out.println("没有cookie=========");  
  15.             } else {  
  16.                 for(Cookie cookie : cookies){  
  17.                     System.out.println("name:"+cookie.getName()+",value:"+ cookie.getValue());  
  18.                 }  
  19.             }  
  20.                
  21.         }    

iew plain[java] view plaincopy

  1. [java] view plain
  2. /** 
  3.          * 添加cookie 
  4.          * @param response 
  5.          * @param name 
  6.          * @param value 
  7.          */  
  8.         @RequestMapping("/addCookie")  
  9.         public void addCookie(HttpServletResponse response,String name,String value){  
  10.             Cookie cookie = new Cookie(name.trim(), value.trim());  
  11.             cookie.setMaxAge(30 * 60);// 设置为30min  
  12.             cookie.setPath("/");  
  13.             System.out.println("已添加===============");  
  14.             response.addCookie(cookie);  
  15.         }  
  1. [java] view plain
  2. /** 
  3.         * 修改cookie 
  4.         * @param request 
  5.         * @param response 
  6.         * @param name 
  7.         * @param value 
  8.         * 注意一、修改、删除Cookie时,新建的Cookie除value、maxAge之外的所有属性,例如name、path、domain等,
  9. 都要与原Cookie完全一样。否则,浏览器将视为两个不同的Cookie不予覆盖,导致修改、删除失败。 
  10.         */  
  11.        @RequestMapping("/editCookie")  
  12.        public void editCookie(HttpServletRequest request,HttpServletResponse response,String name,String value){  
  13.            Cookie[] cookies = request.getCookies();  
  14.            if (null==cookies) {  
  15.                System.out.println("没有cookie==============");  
  16.            } else {  
  17.                for(Cookie cookie : cookies){  
  18.                    if(cookie.getName().equals(name)){  
  19.                        System.out.println("原值为:"+cookie.getValue());  
  20.                        cookie.setValue(value);  
  21.                        cookie.setPath("/");  
  22.                        cookie.setMaxAge(30 * 60);// 设置为30min  
  23.                        System.out.println("被修改的cookie名字为:"+cookie.getName()+",新值为:"+cookie.getValue());  
  24.                        response.addCookie(cookie);  
  25.                        break;  
  26.                    }  
  27.                }  
  28.            }  
  29.               
  30.        }   copy
  1. [java] view plain
  2. /** 
  3.          * 删除cookie 
  4.          * @param request 
  5.          * @param response 
  6.          * @param name 
  7.          */  
  8.         @RequestMapping("/delCookie")  
  9.         public void delCookie(HttpServletRequest request,HttpServletResponse response,String name){  
  10.             Cookie[] cookies = request.getCookies();  
  11.             if (null==cookies) {  
  12.                 System.out.println("没有cookie==============");  
  13.             } else {  
  14.                 for(Cookie cookie : cookies){  
  15.                     if(cookie.getName().equals(name)){  
  16.                         cookie.setValue(null);  
  17.                         cookie.setMaxAge(0);// 立即销毁cookie  
  18.                         cookie.setPath("/");  
  19.                         System.out.println("被删除的cookie名字为:"+cookie.getName());  
  20.                         response.addCookie(cookie);  
  21.                         break;  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  

copy

  1. [java] view plain
  2.      /** 
  3.      * 根据名字获取cookie 
  4.      * @param request 
  5.      * @param name cookie名字 
  6.      * @return 
  7.      */  
  8.     public  Cookie getCookieByName(HttpServletRequest request,String name){  
  9.         Map<String,Cookie> cookieMap = ReadCookieMap(request);  
  10.         if(cookieMap.containsKey(name)){  
  11.             Cookie cookie = (Cookie)cookieMap.get(name);  
  12.             return cookie;  
  13.         }else{  
  14.             return null;  
  15.         }    
  16.     }  

[java] view plain copy

  1.     /** 
  2.     * 将cookie封装到Map里面 
  3.     * @param request 
  4.     * @return 
  5.     */  
  6.    private  Map<String,Cookie> ReadCookieMap(HttpServletRequest request){   
  7.        Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();  
  8.        Cookie[] cookies = request.getCookies();  
  9.        if(null!=cookies){  
  10.            for(Cookie cookie : cookies){  
  11.                cookieMap.put(cookie.getName(), cookie);  
  12.            }  
  13.        }  
  14.        return cookieMap;  
  15.    }  
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页