java代码简单cookie代码
/** * @param key * @param MaxAge * @param cookieValue * @param response */ public static void saveCookie(String key,int MaxAge, String cookieValue,HttpServletResponse response){ Cookie cookie = new Cookie(key, cookieValue); cookie.setMaxAge(MaxAge); cookie.setPath("/"); response.addCookie(cookie); } //清除Cookie public static void clearCookie(String key, HttpServletResponse response){ Cookie cookie = new Cookie(key, null); cookie.setMaxAge(0); cookie.setPath("/"); response.addCookie(cookie); } /** * 获得所要的cookie */ public static String getCookie(String key,HttpServletRequest request){ Cookie cookie[] =request.getCookies(); String cookieValue = null; if(cookie!=null){ for(int i=0;i<cookie.length;i++){ if(key.equals(cookie[i].getName())){ cookieValue =cookie[i].getValue(); break; } } } return cookieValue; }