关闭

cookie 工具类

132人阅读 评论(0) 收藏 举报
分类:
public static final int ONE_SESSION = -1;  //一次会话
public static final int ONE_MONTH = 30*24*60*60;  //一个月
public static final int TWO_HOURS = 2*60*60;      //2小时
public static final int SIX_HOURS = 6*60*60;      //6小时

public static final void clearCookie(HttpServletResponse response, String path, String cookieName) {
   Cookie cookie = new Cookie(cookieName, null);
   cookie.setMaxAge(0);
   cookie.setPath(path);
   response.addCookie(cookie);
}

/**
 * 添加页面端cookie
 * @param response
 * @param cookiename
 * @param cookievalue
 * @param path    设置路径,这个路径即该工程下都可以访问该cookie 如果不设置路径,那么只有设置该cookie路径及其子路径可以访问
 * @param maxSecond
 */
public static final void addCookie(HttpServletResponse response, String cookiename, String cookievalue, String path, int maxSecond){
   Cookie cookie = new Cookie(cookiename, cookievalue);
   cookie.setPath(path);
   cookie.setMaxAge(maxSecond);// 24 hour
   //cookie.setHttpOnly(true);
   response.addCookie(cookie);
}

/**
 * 添加cookie
 * @param key
 * @param value
 * @param response
 */
public static void addCookie(String key, String value, HttpServletResponse response, Integer age){
   Cookie cookie = new Cookie(key, value);
   cookie.setMaxAge(age);
   cookie.setPath("/");
   cookie.setDomain(".che.com");
   response.addCookie(cookie);
}

/**
 * 添加cookie
 * @param key
 * @param value
 * @param response
 */
public static void addCookie(String key, String value, HttpServletResponse response){
    addCookie(key, value, response, 60 * 60 * 24 * 7);
}


public static final Cookie getCookie(HttpServletRequest request, String cookiename) {
   Cookie cookies[] = request.getCookies();
   if (cookies == null)
      return null;
   for (Cookie cookie : cookies) {
      if (cookiename.equals(cookie.getName())) {
         return cookie;
      }
   }
   return null;
}
public static final String getCookieValue(HttpServletRequest request, String cookiename) {
   Cookie cookie = getCookie(request, cookiename);
   if (cookie == null)
      return null;
   return cookie.getValue();
}
0
0
查看评论

常用工具类之cookie

package com.chenfeng.ego.commons.util; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import jav...
  • u012878380
  • u012878380
  • 2017-01-07 11:21
  • 1315

JAVA操作Cookie工具类

import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //import org.apa...
  • wangjun75512
  • wangjun75512
  • 2016-02-15 17:50
  • 537

操作 Cookie 的Java工具类

/** * cn.itcast.bbs.util.CookieUtil * 2014-5-26 * Cookie操作工具类 * gyy */ package cn.itcast.bbs.util; import javax.servlet.http.Cookie; import java...
  • com185272358
  • com185272358
  • 2014-05-26 16:01
  • 1757

Java Cookie工具类代码

Java Cookie工具类代码 添加cookie(设置有效时间) 检索所有cookie 封装到map集合 以其cookie name作为key cookie value作为value 通过cookie name 获取 cookie value import java.util.HashMap; i...
  • xiaokui_wingfly
  • xiaokui_wingfly
  • 2016-07-21 16:25
  • 931

cookieUtil.js

function cookieUtil() { this.getCookie = getCookie; this.setCookie = setCookie; this.checkCookie = checkCookie; this.delCookie=delCookie; th...
  • CTLLIN
  • CTLLIN
  • 2014-05-15 03:28
  • 2480

常用cookie处理方法工具类

功能:cookie的添加、删除、获取值 1 import java.io.UnsupportedEncodingException; 2 import java.net.URLDecoder; 3 4 import javax.servlet.http.Cookie; 5 im...
  • qq_27376871
  • qq_27376871
  • 2018-01-06 14:34
  • 46

工具类:Cookie 工具类

package com.taotao.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.s...
  • qq_34590943
  • qq_34590943
  • 2016-09-16 19:15
  • 173

Cookie工具类

/**  * Cookie工具类  *   */ public class cookieUtil {     private cookieUtil() {     }   ...
  • hanwenyi520
  • hanwenyi520
  • 2015-11-21 17:06
  • 201

cookie 工具类

package org.sms.util; import javax.servlet.http.Cookie; /** * cookie 工具类 * @author kingschan *2014-1-23 */ public class CookieUtil { /** * 得到...
  • u011633363
  • u011633363
  • 2014-02-14 11:11
  • 618

cookie工具类

 function CookieUtil(){}//写CookieCookieUtil.prototype.createCookie = function(name,value,days){    if (days)    {&#...
  • binwei120
  • binwei120
  • 2009-10-20 23:48
  • 261
    个人网站
    个人资料
    • 访问:54181次
    • 积分:1226
    • 等级:
    • 排名:千里之外
    • 原创:69篇
    • 转载:19篇
    • 译文:0篇
    • 评论:9条
    最新评论