You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Marsh, AKP (Kevin) " <K....@rl.ac.uk> on 2003/09/18 14:18:22 UTC

Read/set cookies in .vm files (newbie question)

Hi,
I've inherited a web application where the user interface is created by a
set of velocity macro files (.vm), running under tomcat. I'd like to be able
to read the cookie set by the server and use it to modify the appearence of
the page. I believe it may be possible using velocity tools (using
$toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?), but
have not been able to find any clear examples of this on the web. I'm new to
the velocity/tomcat/java world, and any assistance or advice  on how to do
this would be greatly appreciated !
Many Thanks,
Kevin
 

Re: Read/set cookies in .vm files (newbie question)

Posted by Nathan Bubna <na...@esha.com>.
Marsh, AKP (Kevin) said:
> I've inherited a web application where the user interface is created by a
> set of velocity macro files (.vm), running under tomcat. I'd like to be able
> to read the cookie set by the server and use it to modify the appearence of
> the page. I believe it may be possible using velocity tools (using
> $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?),
...

just so no one gets the wrong idea here...

using the ToolLoader to load the CookieTool will not work properly.  the
ToolLoader does not initialize the tools it "loads" and is pretty much just a
proof-of-concept little utility that is *not* recommended for use in
production.

if you are using the VelocityViewServlet or a subclass thereof, you should be
defining all your tools in the toolbox.xml.  e.g. :

<toolbox>
<tool>
    <key>cookie</key>
    <class>org.apache.velocity.tools.view.tools.CookieTool</class>
</tool>
...
</toolbox>

you would then use the cookie tool as :

## set a new cookie for 30 minutes
$cookie.add('foo', 'bar', 1800)
## get the value of a cookie named 'color'
#set( $color = $cookie.color.value )

Nathan Bubna
nathan@esha.com


Re: Read/set cookies in .vm files (newbie question)

Posted by Terry Steichen <te...@net-frame.com>.
Oops - forgot to mention the Java code also needed (in my "ff" module
below):

 public Cookie createCookie(String name, String value) {
  return new Cookie(name, value);
 }

 public long getCurrentTime() {
  return System.currentTimeMillis();
 }

----- Original Message -----
From: "Terry Steichen" <te...@net-frame.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Thursday, September 18, 2003 9:43 AM
Subject: Re: Read/set cookies in .vm files (newbie question)


> Kevin,
>
> Here's some cookie-related VTL for you to play/experiment with.
>
> Regards,
>
> Terry
>
> ## - get a list of all existing cookies
> #set($cookies = $request.getCookies())
> <b>Cookies</b><br>
> #foreach($cookie in $cookies)
> <b>$velocityCount - $cookie.getName()</b> = $cookie.getValue() Expires:
> $!cookie.getMaxAge() <br>
> #end
> <br><br>
>
> ## - get a list of all existing headers
> <b>Request Headers (from browser)</b><br>
> #set($headers = $request.getHeaderNames())
> #foreach($header in $headers)
> <b>$velocityCount - $header</b> - $request.getHeader($header)<br>
> #end
> <br><br>
>
> ## - create/set a new cookie
> #set($time = "$ff.getCurrentTime()")
> #set($mycookie = $ff.createCookie("Current Milliseconds",$time))
> $mycookie.setMaxAge(1000)
> $mycookie.setPath("/")
> $response.addCookie($mycookie)
>
> ----- Original Message -----
> From: "Marsh, AKP (Kevin) " <K....@rl.ac.uk>
> To: <ve...@jakarta.apache.org>
> Sent: Thursday, September 18, 2003 8:18 AM
> Subject: Read/set cookies in .vm files (newbie question)
>
>
> > Hi,
> > I've inherited a web application where the user interface is created by
a
> > set of velocity macro files (.vm), running under tomcat. I'd like to be
> able
> > to read the cookie set by the server and use it to modify the appearence
> of
> > the page. I believe it may be possible using velocity tools (using
> > $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?),
but
> > have not been able to find any clear examples of this on the web. I'm
new
> to
> > the velocity/tomcat/java world, and any assistance or advice  on how to
do
> > this would be greatly appreciated !
> > Many Thanks,
> > Kevin
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Read/set cookies in .vm files (newbie question)

Posted by Terry Steichen <te...@net-frame.com>.
Oops - forgot to mention the Java code also needed (in my "ff" module
below):

 public Cookie createCookie(String name, String value) {
  return new Cookie(name, value);
 }

 public long getCurrentTime() {
  return System.currentTimeMillis();
 }

----- Original Message -----
From: "Terry Steichen" <te...@net-frame.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Thursday, September 18, 2003 9:43 AM
Subject: Re: Read/set cookies in .vm files (newbie question)


> Kevin,
>
> Here's some cookie-related VTL for you to play/experiment with.
>
> Regards,
>
> Terry
>
> ## - get a list of all existing cookies
> #set($cookies = $request.getCookies())
> <b>Cookies</b><br>
> #foreach($cookie in $cookies)
> <b>$velocityCount - $cookie.getName()</b> = $cookie.getValue() Expires:
> $!cookie.getMaxAge() <br>
> #end
> <br><br>
>
> ## - get a list of all existing headers
> <b>Request Headers (from browser)</b><br>
> #set($headers = $request.getHeaderNames())
> #foreach($header in $headers)
> <b>$velocityCount - $header</b> - $request.getHeader($header)<br>
> #end
> <br><br>
>
> ## - create/set a new cookie
> #set($time = "$ff.getCurrentTime()")
> #set($mycookie = $ff.createCookie("Current Milliseconds",$time))
> $mycookie.setMaxAge(1000)
> $mycookie.setPath("/")
> $response.addCookie($mycookie)
>
> ----- Original Message -----
> From: "Marsh, AKP (Kevin) " <K....@rl.ac.uk>
> To: <ve...@jakarta.apache.org>
> Sent: Thursday, September 18, 2003 8:18 AM
> Subject: Read/set cookies in .vm files (newbie question)
>
>
> > Hi,
> > I've inherited a web application where the user interface is created by
a
> > set of velocity macro files (.vm), running under tomcat. I'd like to be
> able
> > to read the cookie set by the server and use it to modify the appearence
> of
> > the page. I believe it may be possible using velocity tools (using
> > $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?),
but
> > have not been able to find any clear examples of this on the web. I'm
new
> to
> > the velocity/tomcat/java world, and any assistance or advice  on how to
do
> > this would be greatly appreciated !
> > Many Thanks,
> > Kevin
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


Re: Read/set cookies in .vm files (newbie question)

Posted by Terry Steichen <te...@net-frame.com>.
Kevin,

Here's some cookie-related VTL for you to play/experiment with.

Regards,

Terry

## - get a list of all existing cookies
#set($cookies = $request.getCookies())
<b>Cookies</b><br>
#foreach($cookie in $cookies)
<b>$velocityCount - $cookie.getName()</b> = $cookie.getValue() Expires:
$!cookie.getMaxAge() <br>
#end
<br><br>

## - get a list of all existing headers
<b>Request Headers (from browser)</b><br>
#set($headers = $request.getHeaderNames())
#foreach($header in $headers)
<b>$velocityCount - $header</b> - $request.getHeader($header)<br>
#end
<br><br>

## - create/set a new cookie
#set($time = "$ff.getCurrentTime()")
#set($mycookie = $ff.createCookie("Current Milliseconds",$time))
$mycookie.setMaxAge(1000)
$mycookie.setPath("/")
$response.addCookie($mycookie)

----- Original Message -----
From: "Marsh, AKP (Kevin) " <K....@rl.ac.uk>
To: <ve...@jakarta.apache.org>
Sent: Thursday, September 18, 2003 8:18 AM
Subject: Read/set cookies in .vm files (newbie question)


> Hi,
> I've inherited a web application where the user interface is created by a
> set of velocity macro files (.vm), running under tomcat. I'd like to be
able
> to read the cookie set by the server and use it to modify the appearence
of
> the page. I believe it may be possible using velocity tools (using
> $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?), but
> have not been able to find any clear examples of this on the web. I'm new
to
> the velocity/tomcat/java world, and any assistance or advice  on how to do
> this would be greatly appreciated !
> Many Thanks,
> Kevin
>
>


Re: Read/set cookies in .vm files (newbie question)

Posted by Terry Steichen <te...@net-frame.com>.
Kevin,

Here's some cookie-related VTL for you to play/experiment with.

Regards,

Terry

## - get a list of all existing cookies
#set($cookies = $request.getCookies())
<b>Cookies</b><br>
#foreach($cookie in $cookies)
<b>$velocityCount - $cookie.getName()</b> = $cookie.getValue() Expires:
$!cookie.getMaxAge() <br>
#end
<br><br>

## - get a list of all existing headers
<b>Request Headers (from browser)</b><br>
#set($headers = $request.getHeaderNames())
#foreach($header in $headers)
<b>$velocityCount - $header</b> - $request.getHeader($header)<br>
#end
<br><br>

## - create/set a new cookie
#set($time = "$ff.getCurrentTime()")
#set($mycookie = $ff.createCookie("Current Milliseconds",$time))
$mycookie.setMaxAge(1000)
$mycookie.setPath("/")
$response.addCookie($mycookie)

----- Original Message -----
From: "Marsh, AKP (Kevin) " <K....@rl.ac.uk>
To: <ve...@jakarta.apache.org>
Sent: Thursday, September 18, 2003 8:18 AM
Subject: Read/set cookies in .vm files (newbie question)


> Hi,
> I've inherited a web application where the user interface is created by a
> set of velocity macro files (.vm), running under tomcat. I'd like to be
able
> to read the cookie set by the server and use it to modify the appearence
of
> the page. I believe it may be possible using velocity tools (using
> $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?), but
> have not been able to find any clear examples of this on the web. I'm new
to
> the velocity/tomcat/java world, and any assistance or advice  on how to do
> this would be greatly appreciated !
> Many Thanks,
> Kevin
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Read/set cookies in .vm files (newbie question)

Posted by Daniel Dekany <dd...@freemail.hu>.
Thursday, September 18, 2003, 2:18:22 PM, Marsh, AKP (Kevin)  wrote:

> Hi,
> I've inherited a web application where the user interface is created by a
> set of velocity macro files (.vm), running under tomcat. I'd like to be able
> to read the cookie set by the server and use it to modify the appearence of
> the page. I believe it may be possible using velocity tools (using
> $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?), but
> have not been able to find any clear examples of this on the web. I'm new to
> the velocity/tomcat/java world, and any assistance or advice  on how to do
> this would be greatly appreciated !

I think that *usually* it is a bad practice to access cookies,
parameters (query string, from) and such highly technical things from an
MVC-ish template file. It's better to keep the templates "stupid", an
rely on the information exposed by the java code. For example, if you
have a cookie that tells if user wants to see News side-bar or not, then
the vm template should just check boolean variable "showNews" (which was
put into the template context by the java program), and it is problem of
the java controller if it uses a cookie to set that context variable or
maybe an use-profile stored in a DB or what not. The template should
just deal with outputting HTML based on the already prepared data.

-- 
Best regards,
 Daniel Dekany



Re: Read/set cookies in .vm files (newbie question)

Posted by Nathan Bubna <na...@esha.com>.
Marsh, AKP (Kevin) said:
> I've inherited a web application where the user interface is created by a
> set of velocity macro files (.vm), running under tomcat. I'd like to be able
> to read the cookie set by the server and use it to modify the appearence of
> the page. I believe it may be possible using velocity tools (using
> $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?),
...

just so no one gets the wrong idea here...

using the ToolLoader to load the CookieTool will not work properly.  the
ToolLoader does not initialize the tools it "loads" and is pretty much just a
proof-of-concept little utility that is *not* recommended for use in
production.

if you are using the VelocityViewServlet or a subclass thereof, you should be
defining all your tools in the toolbox.xml.  e.g. :

<toolbox>
<tool>
    <key>cookie</key>
    <class>org.apache.velocity.tools.view.tools.CookieTool</class>
</tool>
...
</toolbox>

you would then use the cookie tool as :

## set a new cookie for 30 minutes
$cookie.add('foo', 'bar', 1800)
## get the value of a cookie named 'color'
#set( $color = $cookie.color.value )

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Read/set cookies in .vm files (newbie question)

Posted by Daniel Dekany <dd...@freemail.hu>.
Thursday, September 18, 2003, 2:18:22 PM, Marsh, AKP (Kevin)  wrote:

> Hi,
> I've inherited a web application where the user interface is created by a
> set of velocity macro files (.vm), running under tomcat. I'd like to be able
> to read the cookie set by the server and use it to modify the appearence of
> the page. I believe it may be possible using velocity tools (using
> $toolLoader.load("org.apache.velocity.tools.view.tools.CookieTool" ?), but
> have not been able to find any clear examples of this on the web. I'm new to
> the velocity/tomcat/java world, and any assistance or advice  on how to do
> this would be greatly appreciated !

I think that *usually* it is a bad practice to access cookies,
parameters (query string, from) and such highly technical things from an
MVC-ish template file. It's better to keep the templates "stupid", an
rely on the information exposed by the java code. For example, if you
have a cookie that tells if user wants to see News side-bar or not, then
the vm template should just check boolean variable "showNews" (which was
put into the template context by the java program), and it is problem of
the java controller if it uses a cookie to set that context variable or
maybe an use-profile stored in a DB or what not. The template should
just deal with outputting HTML based on the already prepared data.

-- 
Best regards,
 Daniel Dekany



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org