1. import javax.servlet.http.Cookie;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4.  
  5. /**
  6. * @Package: com.xxxxx.common.util
  7. * @Title: Cookie.java Create on 2012-1-25 下午5:23:52
  8. * @Description:
  9. *
  10. * Cookie工具类,封装Cookie常用操作
  11. *
  12. * @author carrkevin
  13. * @version v 0.1
  14. */
  15. public class CookieHelper {
  16.  
  17. /**
  18. * 设置cookie有效期,根据需要自定义[本系统设置为30天]
  19. */
  20. private final static int COOKIE_MAX_AGE = 1000 * 60 * 60 * 24 * 30;
  21.  
  22. /**
  23. *
  24. * @desc 删除指定Cookie
  25. * @param response
  26. * @param cookie
  27. */
  28. public static void removeCookie(HttpServletResponse response, Cookie cookie)
  29. {
  30. if (cookie != null)
  31. {
  32. cookie.setPath("/");
  33. cookie.setValue("");
  34. cookie.setMaxAge(0);
  35. response.addCookie(cookie);
  36. }
  37. }
  38.  
  39. /**
  40. *
  41. * @desc 删除指定Cookie
  42. * @param response
  43. * @param cookie
  44. * @param domain
  45. */
  46. public static void removeCookie(HttpServletResponse response, Cookie cookie,String domain)
  47. {
  48. if (cookie != null)
  49. {
  50. cookie.setPath("/");
  51. cookie.setValue("");
  52. cookie.setMaxAge(0);
  53. cookie.setDomain(domain);
  54. response.addCookie(cookie);
  55. }
  56. }
  57.  
  58. /**
  59. *
  60. * @desc 根据Cookie名称得到Cookie的值,没有返回Null
  61. * @param request
  62. * @param name
  63. * @return
  64. */
  65. public static String getCookieValue(HttpServletRequest request, String name)
  66. {
  67. Cookie cookie = getCookie(request, name);
  68. if (cookie != null)
  69. {
  70. return cookie.getValue();
  71. }
  72. else
  73. {
  74. return null;
  75. }
  76. }
  77.  
  78. /**
  79. *
  80. * @desc 根据Cookie名称得到Cookie对象,不存在该对象则返回Null
  81. * @param request
  82. * @param name
  83. */
  84. public static Cookie getCookie(HttpServletRequest request, String name)
  85. {
  86. Cookie cookies[] = request.getCookies();
  87. if (cookies == null || name == null || name.length() == 0)
  88. return null;
  89. Cookie cookie = null;
  90. for (int i = 0; i < cookies.length; i++)
  91. {
  92. if (!cookies[i].getName().equals(name))
  93. continue;
  94. cookie = cookies[i];
  95. if (request.getServerName().equals(cookie.getDomain()))
  96. break;
  97. }
  98.  
  99. return cookie;
  100. }
  101.  
  102. /**
  103. *
  104. * @desc 添加一条新的Cookie信息,默认有效时间为一个月
  105. * @param response
  106. * @param name
  107. * @param value
  108. */
  109. public static void setCookie(HttpServletResponse response, String name, String value)
  110. {
  111. setCookie(response, name, value, COOKIE_MAX_AGE);
  112. }
  113.  
  114. /**
  115. *
  116. * @desc 添加一条新的Cookie信息,可以设置其最长有效时间(单位:秒)
  117. * @param response
  118. * @param name
  119. * @param value
  120. * @param maxAge
  121. */
  122. public static void setCookie(HttpServletResponse response, String name, String value, int maxAge)
  123. {
  124. if (value == null)
  125. value = "";
  126. Cookie cookie = new Cookie(name, value);
  127. if(maxAge!=0){
  128. cookie.setMaxAge(maxAge);
  129. }else{
  130. cookie.setMaxAge(COOKIE_MAX_AGE);
  131. }
  132. cookie.setPath("/");
  133. response.addCookie(cookie);
  134. }
  135. }

Cookie 操作工具类的更多相关文章

  1. 170403、java 版cookie操作工具类

    package com.rick.utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; imp ...

  2. java cookie 工具类

    package com.xxx.xxx.xxx.xxx; import java.net.URLDecoder; import java.net.URLEncoder; import javax.se ...

  3. cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题

    最近在解决XSS注入的问题,由于使用的servlet版本是2.5,不支持httpOnly的属性,故做了个工具类来实现cookie的httpOnly的功能.全类如下: /** * cookie工具类,解 ...

  4. Cookie工具类

    import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet ...

  5. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  6. Cookie工具类 - CookieUtil.java

    Cookie工具类,提供Cookie的创建.获取.删除方法. 源码如下:(点击下载 -CookieUtil.java ) import javax.servlet.http.Cookie; impor ...

  7. [转载]C# FTP操作工具类

    本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...

  8. 拼音操作工具类 - PinyinUtil.java

    拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...

  9. Cookie帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Web; namespace AIMSC ...

随机推荐

  1. 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友

     1.拷贝构造 //拷贝构造的规则,有两种方式实现初始化. //1.一个是通过在后面:a(x),b(y)的方式实现初始化. //2.另外一种初始化的方式是直接在构造方法里面实现初始化. 案比例如以 ...

  2. Unix命令操作

    基本命令 [ man 查看 ]--万能命令 1.ls 列出文件 (-al) 2.cd 转换目录 3.mkdir 建立新目录 4.cp 拷贝文件 (-R) 5.rm 删除文件 (-rf) 6.mv 移动 ...

  3. Linux环境编程相关的文章

    Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...

  4. jrtplib的使用

    JRTPLIB库的使用 文档: http://research.edm.uhasselt.be/jori/jrtplib/documentation/index.html 一些介绍: http://d ...

  5. APP-随身听

    简单到复杂听你的专属音响界,听金融.听物业,听新闻和其他节目专辑,简要介绍了新的音频应用,给你不一样的聆听体验.还记得老歌做?这里有.您留声机的一部分!很简单的音频应用,随时随地与此应用程序来听你的私 ...

  6. 第三记“晋IT”分享成长沙龙

    2014年8月17日下午4点-7点,第三期"晋IT"分享成长沙龙在太原大自然蒙特梭利幼儿园多功能厅成功举办. 8月17日下午两点.小编领先来到场地,提前探訪一下准备情况. &quo ...

  7. td中的值自动换行

    设置td中的值自动换行在<td ></td> 中加上这样一句代码,可以简省设置,使长字符串换行.而不用设置width,height. style="word-wrap ...

  8. 初识Python-web框架的这两天

    前段时间打算学习python,其实时间蛮紧张的,看看文字教程,累了就看视频教程.算是把基本的语法过了一遍,但是OOP就费了好大的气力 ,C里有的对象,继承,等等等,还算能理解.不过高级点的就理解起来吃 ...

  9. ventBroker简单实现

    C#编程实践—EventBroker简单实现 前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.n ...

  10. 笔试 - 高德软件有限公司python问题 和 答案

    高德软件有限公司python问题 和 答案 本文地址: http://blog.csdn.net/caroline_wendy/article/details/25230835 by Spike 20 ...