@DefaultKey(value="cookies") @ValidScope(value="request") public class CookieTool extends java.lang.Object
View tool for convenient cookie access and creation.
Template example(s):
$cookie.foo
$cookie.add("bar",'woogie')
tools.xml configuration:
<tools>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.view.CookieTool"/>
</toolbox>
</tools>
This class is only designed for use as a request-scope tool.
| Modifier and Type | Class and Description |
|---|---|
static class |
CookieTool.SugarCookie
Extends
Cookie to add some fluid API sugar and
a toString() method that renders the Cookie's value
instead of the usual Object.toString() shenanigans. |
| Modifier and Type | Field and Description |
|---|---|
private java.util.List<Cookie> |
jar |
protected Log |
log |
protected HttpServletRequest |
request |
protected HttpServletResponse |
response |
| Constructor and Description |
|---|
CookieTool() |
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
add(Cookie c)
Adds the specified Cookie to the HttpServletResponse.
|
java.lang.String |
add(java.lang.String name,
java.lang.String value)
Adds a new Cookie with the specified name and value
to the HttpServletResponse.
|
java.lang.String |
add(java.lang.String name,
java.lang.String value,
java.lang.Object maxAge)
Convenience method to add a new Cookie to the response
and set an expiry time for it.
|
Cookie |
create(java.lang.String name,
java.lang.String value)
Creates a new Cookie with the specified name and value.
|
Cookie |
create(java.lang.String name,
java.lang.String value,
java.lang.Object maxAge)
Convenience method to create a new Cookie
and set an expiry time for it.
|
java.lang.String |
delete(java.lang.String name)
Retrieves the specified cookie and sets the Max-Age to 0
to tell the browser to delete the cookie.
|
Cookie |
get(java.lang.String name)
Returns the Cookie with the specified name, if it exists.
|
java.util.List<Cookie> |
getAll()
Expose array of Cookies for this request to the template.
|
void |
setLog(Log log)
Sets the
Log used for logging messages when Cookie
creation fails due to an invalid name. |
void |
setRequest(HttpServletRequest request)
Sets the current
HttpServletRequest. |
void |
setResponse(HttpServletResponse response)
Sets the current
HttpServletResponse. |
java.lang.String |
toString() |
protected HttpServletRequest request
protected HttpServletResponse response
protected Log log
private java.util.List<Cookie> jar
public void setRequest(HttpServletRequest request)
HttpServletRequest. This is required
for this tool to operate and will throw a NullPointerException
if this is not set or is set to null.public void setResponse(HttpServletResponse response)
HttpServletResponse. This is required
for this tool to operate and will throw a NullPointerException
if this is not set or is set to null.public void setLog(Log log)
Log used for logging messages when Cookie
creation fails due to an invalid name.public java.util.List<Cookie> getAll()
This is equivalent to $request.cookies.
public Cookie get(java.lang.String name)
So, if you had a cookie named 'foo', you'd get it's value
by $cookies.foo.value or it's max age
by $cookies.foo.maxAge
public java.lang.String add(Cookie c)
c - is for cookie (that's good enough for me)public java.lang.String add(java.lang.String name,
java.lang.String value)
name - the name to give this cookievalue - the value to be set for this cookiepublic java.lang.String add(java.lang.String name,
java.lang.String value,
java.lang.Object maxAge)
name - the name to give this cookievalue - the value to be set for this cookiemaxAge - the expiry to be set for this cookiepublic Cookie create(java.lang.String name,
java.lang.String value)
$cookies.add($myCookie).name - the name to give this cookievalue - the value to be set for this cookiepublic Cookie create(java.lang.String name,
java.lang.String value,
java.lang.Object maxAge)
name - the name to give this cookievalue - the value to be set for this cookiemaxAge - the expiry to be set for this cookiepublic java.lang.String delete(java.lang.String name)
name - the name of the cookie to be eatenCookie#setMaxAge,
add(Cookie),
get(String)public java.lang.String toString()
toString in class java.lang.ObjectCopyright (c) 2003-2007 Apache Software Foundation