java与javascript对cookie操作的工具类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java与javascript对cookie操作的工具类相关的知识,希望对你有一定的参考价值。

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;

}

以上是关于java与javascript对cookie操作的工具类的主要内容,如果未能解决你的问题,请参考以下文章

Javascript 操作 cookie

java对cookie的操作

java对cookie的操作

java对cookie的操作

JavaScript之DOM-10 cookie (cookie概述cookie操作)

Web会话跟踪:Cookie与Session