不要在cookie中使用特殊字符的原因分析
更新時間:2010年07月13日 21:05:10 作者:
參數(shù)name需要遵循規(guī)范RFC2109,也就是說name參數(shù)只能包含字母或者數(shù)字,不能包含逗號、分號、空格及美元符號。cookie的name屬性設(shè)置之后不能被修改。
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類構(gòu)造函數(shù)的說明,重點對name和value可以使用的字符進(jìn)行了介紹。
參數(shù)name需要遵循規(guī)范RFC2109,也就是說name參數(shù)只能包含字母或者數(shù)字,不能包含逗號、分號、空格及美元符號。cookie的name屬性設(shè)置之后不能被修改。
參數(shù)value可以是服務(wù)端設(shè)置的任意值,該值可以通過setValue方法進(jìn)行重新設(shè)置。
然后實際情況并非如此??!
(1)value值中不能包含逗號,如果value中包含逗號則程序無法編譯。
如果value中包含逗號,如下:
Cookie pwdCookie = new Cookie("option", "block1,block2");
則異常如下:
java.lang.IllegalArgumentException: block1,block2
在另外一篇關(guān)于JavaScript操作cookie的文章中提到:
在cookie 的名或值中不能使用分號(;)、逗號(,)、等號(=)以及空格。
總之,盡量不要在cookie中使用特殊字符。
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類構(gòu)造函數(shù)的說明,重點對name和value可以使用的字符進(jìn)行了介紹。
參數(shù)name需要遵循規(guī)范RFC2109,也就是說name參數(shù)只能包含字母或者數(shù)字,不能包含逗號、分號、空格及美元符號。cookie的name屬性設(shè)置之后不能被修改。
參數(shù)value可以是服務(wù)端設(shè)置的任意值,該值可以通過setValue方法進(jìn)行重新設(shè)置。
然后實際情況并非如此??!
(1)value值中不能包含逗號,如果value中包含逗號則程序無法編譯。
如果value中包含逗號,如下:
Cookie pwdCookie = new Cookie("option", "block1,block2");
則異常如下:
java.lang.IllegalArgumentException: block1,block2
在另外一篇關(guān)于JavaScript操作cookie的文章中提到:
在cookie 的名或值中不能使用分號(;)、逗號(,)、等號(=)以及空格。
總之,盡量不要在cookie中使用特殊字符。
相關(guān)文章
鼠標(biāo)懸浮在樹組件節(jié)點上展示當(dāng)前節(jié)點名稱的三種實現(xiàn)方式
這篇文章主要介紹了鼠標(biāo)懸浮在樹組件節(jié)點上展示當(dāng)前節(jié)點名稱的三種實現(xiàn)方式,第一種是使用css樣式設(shè)置,第二種在checkBox綁定,第三種使用tooltip,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-12-12在 JavaScript 中保留小數(shù)點后兩位的方法
在 JavaScript 中,有多種方法可以保留小數(shù)點后兩位,本文給大家分享比較常用的方法,文末給大家介紹了實現(xiàn)數(shù)據(jù)格式化保留兩位小數(shù)的多種方法,感興趣的朋友一起看看吧2023-10-10javascript+jQuery實現(xiàn)360開機時間顯示效果
這篇文章主要介紹了javascript+jQuery實現(xiàn)360開機時間顯示效果,在文中給大家提到了js實現(xiàn)時間倒計時的代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11