| < Day Day Up > |
|
The <bean:cookie /> tag is used to retrieve the value of an HTTP cookie. It can be used to retrieve single or multiple cookie values. The retrieved cookie(s) are stored in a page scoped attribute of type Cookie (or Cookie[], if there is more than one HTTP cookie). If the named cookie is not found and no default value is specified, then a request-time exception is thrown.
The <bean:cookie /> tag has no body and supports four attributes, described in Table 20.1.
Attribute | Description |
---|---|
id | Specifies the ID of the scripting variable to be added to the request as a Cookie object. (Required) |
name | Identifies the name of the HTTP cookie being retrieved. (Required) |
multiple | If not null, will cause a Cookie[] containing all of the values for the named HTTP cookie to be returned as opposed to a single Cookie object. If the multiple attribute is not null and there is only a single HTTP cookie, then the first [0] element of the Cookie[] will contain the retrieved value. (Optional) |
value | Specifies the default value to return to store in the javax.servlet.http.Cookie object, if no cookie is found. (Optional) |
Here's an example of using the <bean:cookie /> tag:
<bean:cookie name="userCookie" value="UNKNOWN_USER"/>
In this example, we are looking for a HTTP cookie named userCookie. If the userId cookie exists in the request, then a javax.servlet.http.Cookie object containing the retrieved value is created and stored in the page. Otherwise, a javax.servlet.http.Cookie object containing the string specified in the value attribute-UNKNOWN_USER in this example--is created and stored in the page.
| < Day Day Up > |
|