Java對cookie的操作 package cn.utils; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServl ...
Java對cookie的操作
package cn.utils;
import java.util.HashMap; import java.util.Map;
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 操作cookie的工具類(預設cookie的有效路徑為"/") * @author zwq * @version 1.0 */ public class CookieUtil { /** * 設置cookie * @param response @see javax.servlet.http.HttpServletResponse * @param name cookie名 * @param value cookie值 * @param maxAge cookie的最大活躍時間(單位s) */ public static void setCookie(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); }
/** * 根據cookie名獲取cookie * @param request @see javax.servlet.http.HttpServletRequest * @param name cookie名 * @return */ public static Cookie getCookieByName(HttpServletRequest request,String name){ Map<String,Cookie> cookieMap = getCookieMap(request); if(cookieMap.containsKey(name)){ Cookie cookie = (Cookie)cookieMap.get(name); return cookie; }else{ return null; } } /** * 獲取cookie的map集合(key=cookieName,value=cookie) * @param request @see javax.servlet.http.HttpServletRequest * @return */ private static Map<String,Cookie> getCookieMap(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名檢測cookie是否存在 * @param request @see javax.servlet.http.HttpServletRequest * @param name cookie名 * @return */ public static boolean checkCookieByName(HttpServletRequest request,String name) { if (name != null && name.trim() != "") { Cookie cookie = getCookieByName(request, name); if (cookie != null) { return true; } } return false; }
/** * 根據指定cookie名刪除cookie * @param request @see javax.servlet.http.HttpServletRequest * @param response @see javax.servlet.http.HttpServletResponse * @param name cookie名 */ public static boolean removeCookie(HttpServletRequest request ,HttpServletResponse response,String name) { if (name != null && name.trim() != "") { Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { String cookieName = cookie.getName(); // 找到需要刪除的Cookie if (name.equals(cookieName)) { // 設置生存期為0 cookie.setMaxAge(0); // 設回Response中生效 response.addCookie(cookie); return true; } } } } return false; } } |
Javascript對cookie的操作
/** * @description 操作cookie的工具類 * @author zwq * @version 1.0 */
/** * @description 設置cookie * @param {string} cookieName cookie名 * @param {string} cookieValue cookie值 * @param {Date} [cookieExpires] cookie過期時間(可選參數,預設當前會話) * @param {string} [cookiePath] cookie有效路徑(可選參數,預設"/") */ function setCookie(cookieName, cookieValue, cookieExpires, cookiePath) { if (typeof cookieName === "string" && typeof cookieValue == "string" && cookieName.trim() !== "") { var cookieStr = cookieName + "=" + cookieValue; if (typeof cookieExpires === "object") { cookieStr += ";expires=" + cookieExpires.toUTCString(); } if (typeof cookiePath === "string" && cookiePath.trim() !== "") { cookieStr += ";path=" + cookiePath; } document.cookie = cookieStr; } } /** * @description 根據cookie名獲取cookie值 * @param {string} cookieName cookie名 * @returns {string} */ function getCookie(cookieName) { if (typeof cookieName === "string" && cookieName.trim() !== "") { if (document.cookie.length > 0) { var cookieArr = document.cookie.split(";"); for (var i = 0; i < cookieArr.length; i++) { var cookie = cookieArr[i].trim(); if (cookie.indexOf(cookieName) === 0) { return cookie.substring(cookieName.length + 1, cookie.length); } } } } return ""; } /** * @description 根據指定cookie名檢測cookie是否存在 * @param {string} cookieName cookie名 * @returns {boolean} */ function checkCookie(cookieName) { if (typeof cookieName === "string" && cookieName.trim() !== ""){ var cookieValue = getCookie(cookieName); if (cookieValue !== "" && cookieValue !== null && cookieValue !== undefined){ return true; } } return false; } /** * @description 根據指定cookie名刪除cookie * @param {string} cookieName cookie名 * @returns {boolean} */ function deleteCookie(cookieName) { if (typeof cookieName === "string" && cookieName.trim() !== ""){ var cookieValue = getCookie(cookieName); if (cookieValue !== "" && cookieValue !== null && cookieValue !== undefined){ var date = new Date(); document.cookie = cookieName + "=;expires=" + date.toUTCString(); return true; } } return false; } |