尽量不要在cookie中使用特殊字符


public Cookie(java.lang.String name,java.lang.String value)

Constructs a cookie with a specified name and value.

The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie’s name cannot be changed after creation.

The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie’s value can be changed after creation with the setValue method.

By default, cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion method.

以上是java API文档中对Cookie类构造函数的说明,重点对name和value可以使用的字符进行了介绍。

参数name需要遵循规范RFC2109,也就是说name参数只能包含字母或者数字,不能包含逗号、分号、空格及美元符号。cookie的name属性设置之后不能被修改。

参数value可以是服务端设置的任意值,该值可以通过setValue方法进行重新设置。

然后实际情况并非如此!!

(1)value值中不能包含逗号,如果value中包含逗号则程序无法编译。

如果value中包含逗号,如下:

Cookie pwdCookie = new Cookie("option", "block1,block2");

则异常如下:

java.lang.IllegalArgumentException: block1,block2

在另外一篇关于JavaScript操作cookie的文章中提到:

在cookie 的名或值中不能使用分号(;)、逗号(,)、等号(=)以及空格。

总之,尽量不要在cookie中使用特殊字符。

此条目发表在java/j2ee分类目录,贴了标签。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据