Product Home Page | Developer Site | Version | |
If a cookie with keys is created, as in the following script:
Response.Cookies("myCookie")("type1") = "sugar"
Response.Cookies("myCookie")("type2") = "ginger snap"
the following header is sent:
SET-COOKIE:MYCOOKIE=TYPE1=sugar&TYPE2=ginger+snap
Any subsequent assignment to myCookie
that does not include a key would destroy type1
and type2
. The following example discards the values type1
and type2
and replaces them with the value "chocolate chip
":
Response.Cookies("myCookie") = "chocolate chip"
Conversely, calling a cookie with a key destroys any non-key values the cookie might contain. The following code will discard the value "chocolate chip
" and insert the key value instead:
Response.Cookies("myCookie") ("NewType") = "peanut butter"
To check to see if a cookie has key values, use the following:
Response.Cookies("myCookie").HasKeys
If myCookie
is a dictionary and has keys, the previous script will evaluate to TRUE
, otherwise it will be FALSE
.
An iterator can be used to set cookie attributes. The following example sets all cookies in a collection to expire on Dec. 31, 2003:
<%
For each cookie in Response.Cookies
Response.Cookies(cookie).ExpiresAbsolute = #Dec. 31, 2002#
Next
%>
An iterator can also be used to set the values of all cookies in a collection, or all keys in a cookie. However, when using an iterator to retrieve cookie values, the cookies must have keys or the iterator will not execute. Use the HasKeys property to check to see whether a cookie has any keys. This is demonstrated in the following example.
<%
If Not cookie.HasKeys Then
'Set the value of the cookie
Response.Cookies(cookie) = ""
Else
'Set the value for each key in the cookie collection
For Each key in Response.Cookies(cookie)
Response.Cookies(cookie)(key) = ""
Next key
%>
Copyright © 2003 Sun Microsystems, Inc. All rights reserved.