Java-Cookie源码
- public class Cookie implements Cloneable {
- private static final String LSTRING_FILE =
- "javax.servlet.http.LocalStrings";
- private static ResourceBundle lStrings =
- ResourceBundle.getBundle(LSTRING_FILE);
- //cookie本身的值
- private String name; // NAME= ... "$Name" style is reserved
- private String value; // value of NAME
- //cookie的头部信息
- private String comment; // ;Comment=VALUE ... describes cookie's use
- // ;Discard ... implied by maxAge < 0
- private String domain; // ;Domain=VALUE ... domain that sees cookie
- private int maxAge = -1; // ;Max-Age=VALUE ... cookies auto-expire
- private String path; // ;Path=VALUE ... URLs that see the cookie
- private boolean secure; // ;Secure ... e.g. use SSL
- private int version = 0; // ;Version=1 ... means RFC 2109++ style
- /**
- * Constructs a cookie with a specified name and value.
- *
- * @param name a <code>String</code> specifying the name of the cookie
- *
- * @param value a <code>String</code> specifying the value of the cookie
- *
- * @throws IllegalArgumentException if the cookie name contains illegal characters
- * (for example, a comma, space, or semicolon)
- * or it is one of the tokens reserved for use
- * by the cookie protocol
- * @see #setValue
- * @see #setVersion
- *
- */
- //构造函数,指定特定的name和value
- public Cookie(String name, String value) {
- if (!isToken(name)
- || name.equalsIgnoreCase("Comment") // rfc2019
- || name.equalsIgnoreCase("Discard") // 2019++
- || name.equalsIgnoreCase("Domain")
- || name.equalsIgnoreCase("Expires") // (old cookies)
- || name.equalsIgnoreCase("Max-Age") // rfc2019
- || name.equalsIgnoreCase("Path")
- || name.equalsIgnoreCase("Secure")
- || name.equalsIgnoreCase("Version")
- || name.startsWith("$")
- ) {
- String errMsg = lStrings.getString("err.cookie_name_is_token");
- Object[] errArgs = new Object[1];
- errArgs[0] = name;
- errMsg = MessageFormat.format(errMsg, errArgs);
- throw new IllegalArgumentException(errMsg);
- }
- this.name = name;
- this.value = value;
- }
- /**
- *
- * Specifies a comment that describes a cookie's purpose.
- * The comment is useful if the browser presents the cookie
- * to the user. Comments
- * are not supported by Netscape Version 0 cookies.
- *
- * @param purpose a <code>String</code> specifying the comment
- * to display to the user
- *
- * @see #getComment
- *
- */
- //cookie的说明
- public void setComment(String purpose) {
- comment = purpose;
- }
- //返回cookie数目,为空返回null
- public String getComment() {
- return comment;
- }
- /**
- * Specifies the domain within which this cookie should be presented.
- * <p>The form of the domain name is specified by RFC 2109. A domain
- * name begins with a dot (<code>.foo.com</code>) and means that
- * the cookie is visible to servers in a specified Domain Name System
- * (DNS) zone (for example, <code>www.foo.com</code>, but not
- * <code>a.b.foo.com</code>). By default, cookies are only returned
- * to the server that sent them.
- * @param pattern a <code>String</code> containing the domain name
- * within which this cookie is visible;
- * form is according to RFC 2109
- *
- * @see #getDomain
- */
- //给cookie设置在某个domain中有效
- public void setDomain(String pattern) {
- domain = pattern.toLowerCase(); // IE allegedly needs this
- }
- //获得作用域
- public String getDomain() {
- return domain;
- }
- /**
- * 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
- the <i>maximum</i> age when the cookie will expire, not the cookie's current age.
- * <p>A negative value means
- * that the cookie is not stored persistently and will be deleted
- * when the Web browser exits. A zero value causes the cookie
- * to be deleted.
- * @param expiry an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes
- * the cookie
- * @see #getMaxAge
- */
- //cookie的生存时间
- public void setMaxAge(int expiry) {
- maxAge = expiry;
- }
- //获得最大生存时间
- public int getMaxAge() {
- return maxAge;
- }
- //客户端返回的cookie在某个路径
- public void setPath(String uri) {
- path = uri;
- }
- //返回客户端返回的cookie在server上的地址
- public String getPath() {
- return path;
- }
- /**
- * Indicates to the browser whether the cookie should only be sent
- * using a secure protocol, such as HTTPS or SSL.
- * <p>The default value is <code>false</code>.
- * @param flag if <code>true</code>, sends the cookie from the browser
- * to the server only when using a secure protocol;
- * if <code>false</code>, sent on any protocol
- * @see #getSecure
- */
- public void setSecure(boolean flag) {
- secure = flag;
- }
- public boolean getSecure() {
- return secure;
- }
- //返回cookie的name,一旦创建name不可变
- public String getName() {
- return name;
- }
- //给cookie赋值
- public void setValue(String newValue) {
- value = newValue;
- }
- //获得cookie的值
- public String getValue() {
- return value;
- }
- //返回cookie依从的协议版本
- public int getVersion() {
- return version;
- }
- //设置版本
- public void setVersion(int v) {
- version = v;
- }
- // Note -- disabled for now to allow full Netscape compatibility
- // from RFC 2068, token special case characters
- //
- // private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
- private static final String tspecials = ",; ";
- /*
- * Tests a string and returns true if the string counts as a
- * reserved token in the Java language.
- *
- * @param value the <code>String</code> to be tested
- *
- * @return <code>true</code> if the <code>String</code> is
- * a reserved token; <code>false</code>
- * if it is not
- */
- private boolean isToken(String value) {
- int len = value.length();
- for (int i = 0; i < len; i++) {
- char c = value.charAt(i);
- if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
- return false;
- }
- return true;
- }
- //复制cookie
- public Object clone() {
- try {
- return super.clone();
- } catch (CloneNotSupportedException e) {
- throw new RuntimeException(e.getMessage());
- }
- }
- }
</pre><pre>
Java-Cookie源码的更多相关文章
- Java集合源码分析(四)Vector<E>
Vector<E>简介 Vector也是基于数组实现的,是一个动态数组,其容量能自动增长. Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是 ...
- Java集合源码分析(三)LinkedList
LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...
- Java集合源码分析(二)ArrayList
ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...
- Java集合源码学习(一)集合框架概览
>>集合框架 Java集合框架包含了大部分Java开发中用到的数据结构,主要包括List列表.Set集合.Map映射.迭代器(Iterator.Enumeration).工具类(Array ...
- 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码
转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...
- 【转】Java HashMap 源码解析(好文章)
.fluid-width-video-wrapper { width: 100%; position: relative; padding: 0; } .fluid-width-video-wra ...
- Java Reference 源码分析
@(Java)[Reference] Java Reference 源码分析 Reference对象封装了其它对象的引用,可以和普通的对象一样操作,在一定的限制条件下,支持和垃圾收集器的交互.即可以使 ...
- Eclipse Java 关联源码
今天打代码的时候打算看看Java的源码是怎么实现的 没想到还没关联源码 遇到上面的情况只需要关联下源码就可以对着方法按F3查看JAVA的开源代码. 解决上面如下: 找到jdk的安装目录 找到src.z ...
- 自学Java HashMap源码
自学Java HashMap源码 参考:http://zhangshixi.iteye.com/blog/672697 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提 ...
- Java集合类源码解析:Vector
[学习笔记]转载 Java集合类源码解析:Vector 引言 之前的文章我们学习了一个集合类 ArrayList,今天讲它的一个兄弟 Vector.为什么说是它兄弟呢?因为从容器的构造来说,Vec ...
随机推荐
- 【SSH系列】Hibernate映射 -- 一对一单向关联映射
映射原理 一对一关联映射:两个实体对象之间是一对一的关联映射,即一个对象只能与另外唯一的一个对象相对应.有两种策略可以实现一对一的关联映射: a.主键关联:即让两个对象具有相 ...
- Matplotlib Toolkits:python高级绘图库seaborn
http://blog.csdn.net/pipisorry/article/details/49515745 Seaborn介绍 seaborn (Not distributed with matp ...
- Scala:函数和闭包
http://blog.csdn.net/pipisorry/article/details/52902271 Scala函数 Scala 有函数和方法,二者在语义上的区别很小.Scala 方法是类的 ...
- android开发中使用到的一些设计者模式
单例模式 概念:确保一个类只有一个实例,并且自行实例化并向整个系统提供整个实例. public class Singleton { private static volatile Singleton ...
- 看见的力量 – (II) 影响地图
本文转自台湾的李智桦老师的博客,原文地址 Impact Mapping 真是令人惊艳的可视化工具.等你看完这篇文章,你会爱上它的. 典故 继2011年6月Example of specificatio ...
- spring 的OpenSessionInViewFilter简介
假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterc ...
- 简单RPC实现之Netty实现
所谓RPC就是远程方法调用(Remote Process Call ),简单的来说就是通过MQ,TCP,HTTP或者自己写的网络协议来传输我要调用对方的什么接口,对方处理之后再把结果返回给我.就这么 ...
- 当图片验证码遇上JSP
今天看到了一个关于使用JSP方式生成图片验证码 的小例子,感觉真的是很不错,拿来分享一下. 原理 对于图片验证码,我们在审查元素的时候会方便的看出是<img src="#" ...
- USB有时adb shell连不上设备
USB有时adb shell连不上设备 图1 下面汇总有效的解决方法 1. 重启 2. 卸载和重新装载驱动 图2 3.
- Oracle EBS R12经验谈(二)
作者: jianping.ni 时间: 2009-2-13 12:52 标题: Oracle EBS R12经验谈(二) OAF页面:银行帐户开户人LOV值列表无值 在输入 应付超 ...