@DefaultKey(value="cookies") @ValidScope(value="request") public class CookieTool extends SafeConfig
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 |
---|---|
protected static org.slf4j.Logger |
log |
protected javax.servlet.http.HttpServletRequest |
request |
protected javax.servlet.http.HttpServletResponse |
response |
LOCK_CONFIG_KEY, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
CookieTool() |
Modifier and Type | Method and Description |
---|---|
String |
add(javax.servlet.http.Cookie c)
Adds the specified Cookie to the HttpServletResponse.
|
String |
add(String name,
String value)
Adds a new Cookie with the specified name and value
to the HttpServletResponse.
|
String |
add(String name,
String value,
Object maxAge)
Convenience method to add a new Cookie to the response
and set an expiry time for it.
|
javax.servlet.http.Cookie |
create(String name,
String value)
Creates a new Cookie with the specified name and value.
|
javax.servlet.http.Cookie |
create(String name,
String value,
Object maxAge)
Convenience method to create a new Cookie
and set an expiry time for it.
|
String |
delete(String name)
Retrieves the specified cookie and sets the Max-Age to 0
to tell the browser to delete the cookie.
|
javax.servlet.http.Cookie |
get(String name)
Returns the Cookie with the specified name, if it exists.
|
List<javax.servlet.http.Cookie> |
getAll()
Expose array of Cookies for this request to the template.
|
void |
setRequest(javax.servlet.http.HttpServletRequest request)
Sets the current
HttpServletRequest . |
void |
setResponse(javax.servlet.http.HttpServletResponse response)
Sets the current
HttpServletResponse . |
String |
toString() |
configure, configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
protected static org.slf4j.Logger log
protected javax.servlet.http.HttpServletRequest request
protected javax.servlet.http.HttpServletResponse response
public void setRequest(javax.servlet.http.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
.request
- servlet requestpublic void setResponse(javax.servlet.http.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
.response
- servlet responsepublic List<javax.servlet.http.Cookie> getAll()
This is equivalent to $request.cookies
.
public javax.servlet.http.Cookie get(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
name
- cookie namepublic String add(javax.servlet.http.Cookie c)
c
- is for cookie (that's good enough for me)public String add(String name, String value)
name
- the name to give this cookievalue
- the value to be set for this cookiepublic String add(String name, String value, 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 javax.servlet.http.Cookie create(String name, String value)
$cookies.add($myCookie)
.name
- the name to give this cookievalue
- the value to be set for this cookiepublic javax.servlet.http.Cookie create(String name, String value, 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 String delete(String name)
name
- the name of the cookie to be eatenCookie.setMaxAge(int)
,
add(Cookie)
,
get(String)
Copyright © 2002–2018 The Apache Software Foundation. All rights reserved.