星空网 > 软件开发 > Java

常用cookie处理方法工具类

功能:cookie的添加、删除、获取值

 1 import java.io.UnsupportedEncodingException; 2 import java.net.URLDecoder; 3  4 import javax.servlet.http.Cookie; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7  8 /** 9  * 常用cookie处理方法工具类10 */11 public class CookieUtil {12   13   /**14    * 添加cookie15    * @param response16    * @param key cookie主键17    * @param value cookie值18   */19   public static void addCookie(HttpServletResponse response, String key, String value){20     Cookie cookie = new Cookie(key, value);21     cookie.setPath("/");// 这个要设置 22     cookie.setMaxAge(60*60*24*30);//保留一个月 以秒为单位 23     response.addCookie(cookie);24   }25   26   /**27    * 删除cookie28    * @param request29    * @param response30    * @param key cookie主键31   */32   public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String key){33     Cookie cookies[] = request.getCookies(); 34     if (cookies != null) { 35       for (int i = 0; i < cookies.length; i++) { 36         if (cookies[i].getName().equals(key)) { 37           Cookie cookie = new Cookie(key,null);38           cookie.setPath("/");//设置成跟写入cookies一样的 39           cookie.setMaxAge(0); 40           response.addCookie(cookie); 41         } 42       } 43     } 44   }45   46   /**47    * 取得cookie的值48    * @param request49    * @param key cookie主键50   */51   public static String getCookieValue(HttpServletRequest request, String key) throws UnsupportedEncodingException{52     for(Cookie cookie : request.getCookies()){53       if (cookie.getName().equals(key)) {  54         return URLDecoder.decode(cookie.getValue(), "UTF-8"); 55       } 56     }57     return null;58   }59 }

 




原标题:常用cookie处理方法工具类

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外仓介绍ppt:https://www.goluckyvip.com/tag/95414.html
海外仓库图片:https://www.goluckyvip.com/tag/95416.html
海外仓库有哪些 襄派海外仓:https://www.goluckyvip.com/tag/95419.html
海外仓政策:https://www.goluckyvip.com/tag/9542.html
美国海外仓是如何收费的:https://www.goluckyvip.com/tag/95420.html
海外仓的兴起原因:https://www.goluckyvip.com/tag/95421.html
夹江千佛岩景区门票(夹江千佛岩景区门票价格):https://www.vstour.cn/a/411232.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流