标签:cookie java web开发 jquery html
/** * @Description: * * @Title: CookieUtil.java * @Package com.hupu.nac.util * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-1-15 上午10:17:51 * @version V2.0 */ package com.hupu.nac.util; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @Description: Cookie读写 * * @ClassName: CookieUtil * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-1-15 上午10:17:51 * @version V2.0 */ public class CookieUtil { /** * @Description: 添加cookie * * @param response * @param name * @param value * @param maxAge * @return * * @Title: CookieUtil.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-1-15 上午10:29:29 * @version V2.0 */ public static Cookie addCookie(HttpServletResponse response,String name,String value,int maxAge){ Cookie cookie = new Cookie(name,value); cookie.setPath("/"); if(maxAge > 0){ cookie.setMaxAge(maxAge); } response.addCookie(cookie); return cookie; } /** * @Description: 根据key获取Cookie对象 * * @param request * @param key * @return * * @Title: CookieUtil.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-1-15 上午10:29:44 * @version V2.0 */ public static Cookie getCookieByName(HttpServletRequest request,String key){ Map<String,Cookie> cookieMap = readCookieMap(request); if(cookieMap.containsKey(key)){ Cookie cookie = (Cookie)cookieMap.get(key); return cookie; }else{ return null; } } /** * @Description: 封装Cookie读取方法 * * @param request * @return * * @Title: CookieUtil.java * @Copyright: Copyright (c) 2014 * * @author Comsys-LZP * @date 2014-1-15 上午10:30:05 * @version V2.0 */ private static Map<String,Cookie> readCookieMap(HttpServletRequest request){ Map<String,Cookie> cookieMap = new HashMap<String,Cookie>(); Cookie[] cookies = request.getCookies(); if(null != cookies){ for(Cookie cookie : cookies){ cookieMap.put(cookie.getName(), cookie); } } return cookieMap; } }
标签:cookie java web开发 jquery html
原文地址:http://blog.csdn.net/luo201227/article/details/39077143