Cookie: COOKIE1; COOKIE2=abc
In a CFML page we process the cookies but are getting the following exception
when attempting to access the values of cookies which have been submitted
without a value:
Element COOKIE1 is undefined in a Java object of type class
coldfusion.runtime.CookieScope referenced as The error occurred on line 4.
The following is a CFML example that will reproduce this behavior:
<CFSCRIPT>
cScope = Evaluate('COOKIE');
for ( sField in cScope ) {
WriteOutput(sField & '=' & cScope[sField]);
}
</CFSCRIPT>
If however, the HTTP Cookie header is formatted as follows, CF does not have a
problem retrieving its value, even though that value is empty.
Cookie: COOKIE1=; COOKIE2=abc
To me it seems that if CF is able to report that a field exists within a
collection, such as from the statement for ( sField in cScope ) {, then it
should also not have an issue accessing the same field.
Is this defined behavior?
Thanks in advance,
Jeremy
<!--- an empty string is a valid value --->
<cfset myVariable = "" />
If the key's value is null, it "essentially" does not exist to CF and so it
throws an undefined error when you try to access it. Try using IsDefined() to
determine whether or not the key's value is null.
Are you asking why the behavior is different than with regular structures?
"COOKIE" is not a true CF structure (ie. object created with StructNew()), so
the behavior is a bit different.
Java collections use key/value pairs. In this situation the "key" (COOKIE1)
exists, but the key's "value" is null because it was not explicitly set to an
empty string as it was in the second statement.
Cookie: COOKIE1=; COOKIE2=abc