Class CookieTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.view.CookieTool
@DefaultKey("cookies")
@ValidScope("request")
public class CookieTool
extends org.apache.velocity.tools.generic.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.
- Since:
- VelocityTools 2.0
- Version:
- $Id$
- Author:
- Dmitri Colebatch, Nathan Bubna
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classExtendsCookieto add some fluid API sugar and a toString() method that renders the Cookie's value instead of the usual Object.toString() shenanigans. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static org.slf4j.Loggerprotected javax.servlet.http.HttpServletRequestprotected javax.servlet.http.HttpServletResponseFields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a new Cookie with the specified name and value to the HttpServletResponse.Convenience method to add a new Cookie to the response and set an expiry time for it.add(javax.servlet.http.Cookie c) Adds the specified Cookie to the HttpServletResponse.javax.servlet.http.CookieCreates a new Cookie with the specified name and value.javax.servlet.http.CookieConvenience method to create a new Cookie and set an expiry time for it.Retrieves the specified cookie and sets the Max-Age to 0 to tell the browser to delete the cookie.javax.servlet.http.CookieReturns 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.voidsetRequest(javax.servlet.http.HttpServletRequest request) Sets the currentHttpServletRequest.voidsetResponse(javax.servlet.http.HttpServletResponse response) Sets the currentHttpServletResponse.toString()Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
Field Details
-
log
protected static org.slf4j.Logger log -
request
protected javax.servlet.http.HttpServletRequest request -
response
protected javax.servlet.http.HttpServletResponse response
-
-
Constructor Details
-
CookieTool
public CookieTool()
-
-
Method Details
-
setRequest
public void setRequest(javax.servlet.http.HttpServletRequest request) Sets the currentHttpServletRequest. This is required for this tool to operate and will throw a NullPointerException if this is not set or is set tonull.- Parameters:
request- servlet request
-
setResponse
public void setResponse(javax.servlet.http.HttpServletResponse response) Sets the currentHttpServletResponse. This is required for this tool to operate and will throw a NullPointerException if this is not set or is set tonull.- Parameters:
response- servlet response
-
getAll
Expose array of Cookies for this request to the template.This is equivalent to
$request.cookies.- Returns:
- list of Cookie objects for this request
-
get
Returns the Cookie with the specified name, if it exists.So, if you had a cookie named 'foo', you'd get it's value by
$cookies.foo.valueor it's max age by$cookies.foo.maxAge- Parameters:
name- cookie name- Returns:
- found cookie or null
-
add
Adds the specified Cookie to the HttpServletResponse. This does *not* add the Cookie to the current request.- Parameters:
c- is for cookie (that's good enough for me)- Returns:
- an empty String to prevent the reference from rendering unless the cookie passed in is null, then it returns null
- Since:
- VelocityTools 2.0
-
add
Adds a new Cookie with the specified name and value to the HttpServletResponse. This does *not* add a Cookie to the current request.- Parameters:
name- the name to give this cookievalue- the value to be set for this cookie- Returns:
- an empty String to prevent the reference from rendering
-
add
Convenience method to add a new Cookie to the response and set an expiry time for it.- Parameters:
name- the name to give this cookievalue- the value to be set for this cookiemaxAge- the expiry to be set for this cookie- Returns:
- an empty String to prevent the reference from rendering
-
create
Creates a new Cookie with the specified name and value. This does *not* add the Cookie to the response, so the created Cookie will not be set unless you do$cookies.add($myCookie).- Parameters:
name- the name to give this cookievalue- the value to be set for this cookie- Returns:
- The new SugarCookie object.
- Since:
- VelocityTools 1.3
-
create
Convenience method to create a new Cookie and set an expiry time for it.- Parameters:
name- the name to give this cookievalue- the value to be set for this cookiemaxAge- the expiry to be set for this cookie- Returns:
- The new SugarCookie object.
- Since:
- VelocityTools 1.3
-
delete
Retrieves the specified cookie and sets the Max-Age to 0 to tell the browser to delete the cookie. Then this returns an empty string to make this render silently. If no such cookie exists, then it returns null to show the error.- Parameters:
name- the name of the cookie to be eaten- Returns:
- empty string, or null if no such cookie exists
- See Also:
-
toString
-