org.apache.velocity.tools.view.tools
Class CookieTool

java.lang.Object
  extended by org.apache.velocity.tools.view.tools.CookieTool

public class CookieTool
extends java.lang.Object

View tool for convenient cookie access and creation.

 Template example(s):
  $cookie.foo.value
  $cookie.add("bar",'woogie')

 Toolbox configuration:
 <tool>
   <key>cookie</key>
   <scope>request</scope>
   <class>org.apache.velocity.tools.view.tools.CookieTool</class>
 </tool>
 

This class is only designed for use as a request-scope tool.

Since:
VelocityTools 1.1
Version:
$Id: CookieTool.java 485171 2006-12-10 12:49:08Z henning $
Author:
Dmitri Colebatch, Nathan Bubna

Field Summary
protected  javax.servlet.http.HttpServletRequest request
           
protected  javax.servlet.http.HttpServletResponse response
           
 
Constructor Summary
CookieTool()
           
 
Method Summary
 void add(java.lang.String name, java.lang.String value)
          Adds a new Cookie with the specified name and value to the HttpServletResponse.
 void 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.
 javax.servlet.http.Cookie create(java.lang.String name, java.lang.String value)
          Creates a new Cookie with the specified name and value.
 javax.servlet.http.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.
 javax.servlet.http.Cookie get(java.lang.String name)
          Returns the Cookie with the specified name, if it exists.
 javax.servlet.http.Cookie[] getAll()
          Expose array of Cookies for this request to the template.
 void init(java.lang.Object obj)
          Initializes this instance for the current request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

protected javax.servlet.http.HttpServletRequest request

response

protected javax.servlet.http.HttpServletResponse response
Constructor Detail

CookieTool

public CookieTool()
Method Detail

init

public void init(java.lang.Object obj)
Initializes this instance for the current request.

Parameters:
obj - the ViewContext of the current request

getAll

public javax.servlet.http.Cookie[] getAll()
Expose array of Cookies for this request to the template.

This is equivalent to $request.cookies.

Returns:
array of Cookie objects for this request

get

public javax.servlet.http.Cookie get(java.lang.String name)
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.value or it's max age by $cookies.foo.maxAge


add

public void add(java.lang.String name,
                java.lang.String value)
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 cookie
value - the value to be set for this cookie

add

public void 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.

Parameters:
name - the name to give this cookie
value - the value to be set for this cookie
maxAge - the expiry to be set for this cookie

create

public javax.servlet.http.Cookie create(java.lang.String name,
                                        java.lang.String value)
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 $response.addCookie($myCookie).

Parameters:
name - the name to give this cookie
value - the value to be set for this cookie
Returns:
The new Cookie object.
Since:
VelocityTools 1.3

create

public javax.servlet.http.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.

Parameters:
name - the name to give this cookie
value - the value to be set for this cookie
maxAge - the expiry to be set for this cookie
Returns:
The new Cookie object.
Since:
VelocityTools 1.3


Copyright (c) 2003-2007 Apache Software Foundation