用java操作cookie和用js操作cookie

1:java类,代码如下:

package com.drmedias.util;

import javax.servlet.http.*;
import org.apache.log4j.Logger;

public class CookieUtil
{

    public CookieUtil()
    {
    }

    public static void setCookie(HttpServletResponse response, String name, String value, int expires, String path, String domain)
    {
        logger.debug((new StringBuilder("setCookie:[")).append(name).append(",").append(value).append(",").append(expires).append(",").append(path).append(",").append(domain).append("]").toString());
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(expires);
        if(path != null)
            cookie.setPath(path);
        if(domain != null)
            cookie.setDomain(domain);
        response.addCookie(cookie);
    }

    public static void setCookie(HttpServletResponse response, String name, String value)
    {
        setCookie(response, name, value, 2592000, "/", null);
    }

    public static String getCookieValue(HttpServletRequest request, String name)
    {
        Cookie cookie = getCookie(request, name);
        return cookie != null ? cookie.getValue() : null;
    }

    public static Cookie getCookie(HttpServletRequest request, String name)
    {
        Cookie cookies[] = request.getCookies();
        if(cookies == null)
            return null;
        int i = 0;
        for(int len = cookies.length; i < len; i++)
            if(cookies[i].getName().equals(name))
                return cookies[i];

        return null;
    }

    public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String name)
    {
        logger.debug((new StringBuilder("deleteCookie:[")).append(name).append("]").toString());
        Cookie cookie = getCookie(request, name);
        if(cookie != null)
        {
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
    }

    private static Logger logger = Logger.getLogger(com/drmedias/util/CookieUtil);
    private static final int EXPRI_MONTH = 2592000;
    private static final int EXPRI_END = 0;
    private static final String PATH_ROOT = "/";

}

2:用js操作cookie


function setCookie(name, value, expires, path, domain, secure) {
    var expireDate = new Date();
    var EXPIR_MONTH = 30*24*3600*1000;
    (expires) ? expireDate.setTime(expireDate.getTime() + expires * 1000) : expireDate.setTime(expireDate.getTime() + EXPIR_MONTH);
    document.cookie= name + "=" + escape(value) +
    "; expires=" + expireDate.toGMTString() +
    ((path) ? "; path=" + path : "; path=/") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}


function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值