The Cookietool Velocity Viewtool allows you to create and access cookies. This class is only designed for use as a request-scope tool. It exposes the methods of the standard CookieTool viewtool.
Note: This viewtool cannot be used directly from a static page — e.g., one served via CDN — but can still be utilized through the Scripting API in such a case.
The following snippet shows how the CookieTool Viewtool is mapped in the toolbox.xml file:
<tool>
<key>cookietool</key>
<scope>request</scope>
<class>org.apache.velocity.tools.view.tools.CookieTool</class>
</tool>
Usage
Command | Result |
---|---|
$cookietool.foo or $cookietool.get("foo") | Retrieves the cookie with the Name attribute of foo |
$cookietool.foo.value | Returns the Value of the specified cookie |
$cookietool.foo.name | Returns the Name of the specified cookie ("foo" , in this case) |
$cookietool.add("bar", "woogie"[, 60]) | Adds a cookie with Name bar , Value woogie , and (optional) MaxAge of 60 seconds |
$cookietool.delete("bar") | Sets the MaxAge of cookie bar to 0, causing its deletion |
$cookietool.all | Returns an iterable object of all cookies accessible through the Request object |
Example
The following code prints a bulleted list of all cookies associated with the current Request object.
<ul>
#foreach( $cookie in $cookietool.all )
<li>${cookie.name} = ${cookie.value}</li>
#end
</ul>