1. public class Cookie implements Cloneable {
  2. private static final String LSTRING_FILE =
  3. "javax.servlet.http.LocalStrings";
  4. private static ResourceBundle lStrings =
  5. ResourceBundle.getBundle(LSTRING_FILE);
  6. //cookie本身的值
  7. private String name;    // NAME= ... "$Name" style is reserved
  8. private String value;   // value of NAME
  9. //cookie的头部信息
  10. private String comment; // ;Comment=VALUE ... describes cookie's use
  11. // ;Discard ... implied by maxAge < 0
  12. private String domain;  // ;Domain=VALUE ... domain that sees cookie
  13. private int maxAge = -1;    // ;Max-Age=VALUE ... cookies auto-expire
  14. private String path;    // ;Path=VALUE ... URLs that see the cookie
  15. private boolean secure; // ;Secure ... e.g. use SSL
  16. private int version = 0;    // ;Version=1 ... means RFC 2109++ style
  17. /**
  18. * Constructs a cookie with a specified name and value.
  19. *
  20. * @param name          a <code>String</code> specifying the name of the cookie
  21. *
  22. * @param value         a <code>String</code> specifying the value of the cookie
  23. *
  24. * @throws IllegalArgumentException if the cookie name contains illegal characters
  25. *                  (for example, a comma, space, or semicolon)
  26. *                  or it is one of the tokens reserved for use
  27. *                  by the cookie protocol
  28. * @see #setValue
  29. * @see #setVersion
  30. *
  31. */
  32. //构造函数,指定特定的name和value
  33. public Cookie(String name, String value) {
  34. if (!isToken(name)
  35. || name.equalsIgnoreCase("Comment") // rfc2019
  36. || name.equalsIgnoreCase("Discard") // 2019++
  37. || name.equalsIgnoreCase("Domain")
  38. || name.equalsIgnoreCase("Expires") // (old cookies)
  39. || name.equalsIgnoreCase("Max-Age") // rfc2019
  40. || name.equalsIgnoreCase("Path")
  41. || name.equalsIgnoreCase("Secure")
  42. || name.equalsIgnoreCase("Version")
  43. || name.startsWith("$")
  44. ) {
  45. String errMsg = lStrings.getString("err.cookie_name_is_token");
  46. Object[] errArgs = new Object[1];
  47. errArgs[0] = name;
  48. errMsg = MessageFormat.format(errMsg, errArgs);
  49. throw new IllegalArgumentException(errMsg);
  50. }
  51. this.name = name;
  52. this.value = value;
  53. }
  54. /**
  55. *
  56. * Specifies a comment that describes a cookie's purpose.
  57. * The comment is useful if the browser presents the cookie
  58. * to the user. Comments
  59. * are not supported by Netscape Version 0 cookies.
  60. *
  61. * @param purpose       a <code>String</code> specifying the comment
  62. *              to display to the user
  63. *
  64. * @see #getComment
  65. *
  66. */
  67. //cookie的说明
  68. public void setComment(String purpose) {
  69. comment = purpose;
  70. }
  71. //返回cookie数目,为空返回null
  72. public String getComment() {
  73. return comment;
  74. }
  75. /**
  76. * Specifies the domain within which this cookie should be presented.
  77. * <p>The form of the domain name is specified by RFC 2109. A domain
  78. * name begins with a dot (<code>.foo.com</code>) and means that
  79. * the cookie is visible to servers in a specified Domain Name System
  80. * (DNS) zone (for example, <code>www.foo.com</code>, but not
  81. * <code>a.b.foo.com</code>). By default, cookies are only returned
  82. * to the server that sent them.
  83. * @param pattern       a <code>String</code> containing the domain name
  84. *              within which this cookie is visible;
  85. *              form is according to RFC 2109
  86. *
  87. * @see #getDomain
  88. */
  89. //给cookie设置在某个domain中有效
  90. public void setDomain(String pattern) {
  91. domain = pattern.toLowerCase(); // IE allegedly needs this
  92. }
  93. //获得作用域
  94. public String getDomain() {
  95. return domain;
  96. }
  97. /**
  98. * Sets the maximum age of the cookie in seconds. <p>A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is
  99. the <i>maximum</i> age when the cookie will expire, not the cookie's current age.
  100. * <p>A negative value means
  101. * that the cookie is not stored persistently and will be deleted
  102. * when the Web browser exits. A zero value causes the cookie
  103. * to be deleted.
  104. * @param expiry        an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes
  105. *  the cookie
  106. * @see #getMaxAge
  107. */
  108. //cookie的生存时间
  109. public void setMaxAge(int expiry) {
  110. maxAge = expiry;
  111. }
  112. //获得最大生存时间
  113. public int getMaxAge() {
  114. return maxAge;
  115. }
  116. //客户端返回的cookie在某个路径
  117. public void setPath(String uri) {
  118. path = uri;
  119. }
  120. //返回客户端返回的cookie在server上的地址
  121. public String getPath() {
  122. return path;
  123. }
  124. /**
  125. * Indicates to the browser whether the cookie should only be sent
  126. * using a secure protocol, such as HTTPS or SSL.
  127. * <p>The default value is <code>false</code>.
  128. * @param flag  if <code>true</code>, sends the cookie from the browser
  129. *          to the server only when using a secure protocol;
  130. *          if <code>false</code>, sent on any protocol
  131. * @see #getSecure
  132. */
  133. public void setSecure(boolean flag) {
  134. secure = flag;
  135. }
  136. public boolean getSecure() {
  137. return secure;
  138. }
  139. //返回cookie的name,一旦创建name不可变
  140. public String getName() {
  141. return name;
  142. }
  143. //给cookie赋值
  144. public void setValue(String newValue) {
  145. value = newValue;
  146. }
  147. //获得cookie的值
  148. public String getValue() {
  149. return value;
  150. }
  151. //返回cookie依从的协议版本
  152. public int getVersion() {
  153. return version;
  154. }
  155. //设置版本
  156. public void setVersion(int v) {
  157. version = v;
  158. }
  159. // Note -- disabled for now to allow full Netscape compatibility
  160. // from RFC 2068, token special case characters
  161. //
  162. // private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
  163. private static final String tspecials = ",; ";
  164. /*
  165. * Tests a string and returns true if the string counts as a
  166. * reserved token in the Java language.
  167. *
  168. * @param value     the <code>String</code> to be tested
  169. *
  170. * @return          <code>true</code> if the <code>String</code> is
  171. *              a reserved token; <code>false</code>
  172. *              if it is not
  173. */
  174. private boolean isToken(String value) {
  175. int len = value.length();
  176. for (int i = 0; i < len; i++) {
  177. char c = value.charAt(i);
  178. if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
  179. return false;
  180. }
  181. return true;
  182. }
  183. //复制cookie
  184. public Object clone() {
  185. try {
  186. return super.clone();
  187. } catch (CloneNotSupportedException e) {
  188. throw new RuntimeException(e.getMessage());
  189. }
  190. }
  191. }
</pre><pre>

												

Java-Cookie源码的更多相关文章

  1. Java集合源码分析(四)Vector<E>

    Vector<E>简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长. Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是 ...

  2. Java集合源码分析(三)LinkedList

    LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...

  3. Java集合源码分析(二)ArrayList

    ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...

  4. Java集合源码学习(一)集合框架概览

    >>集合框架 Java集合框架包含了大部分Java开发中用到的数据结构,主要包括List列表.Set集合.Map映射.迭代器(Iterator.Enumeration).工具类(Array ...

  5. 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码

    转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...

  6. 【转】Java HashMap 源码解析(好文章)

    ­ .fluid-width-video-wrapper { width: 100%; position: relative; padding: 0; } .fluid-width-video-wra ...

  7. Java Reference 源码分析

    @(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...

  8. Eclipse Java 关联源码

    今天打代码的时候打算看看Java的源码是怎么实现的 没想到还没关联源码 遇到上面的情况只需要关联下源码就可以对着方法按F3查看JAVA的开源代码. 解决上面如下: 找到jdk的安装目录 找到src.z ...

  9. 自学Java HashMap源码

    自学Java HashMap源码 参考:http://zhangshixi.iteye.com/blog/672697 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提 ...

  10. Java集合类源码解析:Vector

    [学习笔记]转载 Java集合类源码解析:Vector   引言 之前的文章我们学习了一个集合类 ArrayList,今天讲它的一个兄弟 Vector.为什么说是它兄弟呢?因为从容器的构造来说,Vec ...

随机推荐

  1. Android实现多条Toast快速显示(强制中止上一条Toast的显示)

    Android实现多条Toast快速显示 Toast多用于我们开发人员调试使用,有时候也作为给用户的弱提示使用,我们常用的方法是 Toast.makeText(this, "弹出Toast& ...

  2. Spark技术内幕:Shuffle Read的整体流程

    回忆一下,每个Stage的上边界,要么需要从外部存储读取数据,要么需要读取上一个Stage的输出:而下边界,要么是需要写入本地文件系统(需要Shuffle),以供childStage读取,要么是最后一 ...

  3. Android简易实战教程--第三十话《撕衣美女》

    此篇邪恶一些,给单身屌丝发点"福利",通过图片的绘制,给美女脱掉衣服. 原理:图片覆盖图片,通过画笔对顶端的图片做一些特效处理,即手指触摸的地方,设置为透明.即可显示最底部的美女图 ...

  4. 21 viewPager--- hzScrollView ----llContainer

    结构: MainActivity.java package com.qf.day21_hsviewpagerfragment_demo5; import java.util.ArrayList; im ...

  5. Map俩种遍历方式

    Map本身没有迭代器因而在遍历其中元素时需要采取新的措施,在JDK中提供了俩种方法 keySet Set<K> keySet() 返回此映射中包含的键的 Set 视图.该 set 受映射支 ...

  6. 剑指Offer——线程同步volatile与synchronized详解

    (转)Java面试--线程同步volatile与synchronized详解 0. 前言 面试时很可能遇到这样一个问题:使用volatile修饰int型变量i,多个线程同时进行i++操作,这样可以实现 ...

  7. spark shuffle

    Spark Shuffle 1. Shuffle相关 当Map的输出结果要被Reduce使用时,输出结果需要按key哈希,并且分发到每一个Reducer上去,这个过程就是shuffle.由于shuff ...

  8. 【Android应用开发】Android Studio 错误集锦 -- 将所有的 AS 错误集合到本文

    . 一. 编译错误 1. "AndroidManifest.xml file not found" 错误 (1) 报错信息 报错信息 : -- Message Make : Inf ...

  9. 如何禁止App在后台运行以及如何保存和恢复App的状态

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...

  10. android 使用Vysor投影到电脑

    有没有好的投影软件可以将android屏幕投影到电脑,当然这种很多,比如360就自带了投影功能,小米盒子也可以(不过貌似只能支持到4.4版本),今天要说的是Vysor,google的一款投影软件. V ...