You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by William Fuller <wi...@sbcglobal.net> on 2005/01/23 12:21:02 UTC

Best way to use Velocity

I have Derby feeding XML documents into Digester, that creates a bunch of
threads that are pushlet publishers and populates a tree of vectors and
properties.  That all works just fine with hand-made HTML files (I got rid
of the JSP from pushlets by using an applet calling JavaScript).  Now I need
to generate the HTML from that same data tree.

To do this, I need to simply do a getInstance and that will give me a
pointer to the root of the tree.  The data was already loaded by another
servlet that runs at startup of Tomcat.

My question is, what is the best way to use Velocity in this context?  I
need to get the data instance before the template is rendered.  Should I use
VelocityView and derrive a class from that to get my data, or should I just
go straight Velocity and load it with the invoker?  Security isn't a
consideration here, because this is an embedded application.

Also, is there a good web site with Velocity examples?  The examples beyond
the trivial examples in the distribution?


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


Re: Using the session object to store Velocity engines

Posted by Will Glass-Husain <wg...@forio.com>.
For some sample code and ideas for customizing Velocity, see the Powerpoint 
slides and sample code Hacking Velocity.

http://wiki.apache.org/jakarta-velocity/HackingVelocity

WILL

----- Original Message ----- 
From: "Steve O'Hara" <so...@pivotal-solutions.co.uk>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Monday, January 24, 2005 8:04 AM
Subject: RE: Using the session object to store Velocity engines


> Thanks Simon for your info.  I've got seperate Vlocity instances because 
> I'm
> serving up templates from different root directories.
> I'm fairly new to Velocity (and Java come to that) so I hadn't considered
> delving into the realms of a custom loader.  That would certainly allow me
> to run Velocity as a singleton again which would negate the reasons for
> storing the instances in the session object.
>
> Thanks
>
> Steve
>
> -----Original Message-----
> From: Simon Christian [mailto:simon@stoutstick.com]
> Sent: 24 January 2005 15:45
> To: Velocity Users List
> Subject: Re: Using the session object to store Velocity engines
>
>
> Hi Steve,
>
> Steve O'Hara wrote:
>> Hi,
>>
>> My Velocity app uses standalone Velocity instances (I've got different
> Init
>> requirments depending upon the request) - I'm wondering what the effect 
>> on
>> performance is of putting these instances into the session object rather
>> than instantiating them on every request?  Im using Tomcat 4.x
>>
>> My background is ASP/VB/IIS where storing objects (COM) in the session
>> object is a performance no-no because of threading issues in the
> container.
>> Is this something that Tomcat/Java can do without major penalty?  Has
> anyone
>> any experience with this sort of thing?
>>
>> Thanks,
>>
>> Steve
>>
>
> Can I ask why you might need to create a new instance of Velocity per
> request (as your message implies)? The instantiation of the Velocity is
> a (relatively) slow process compared to actually *using* it.
>
> Three possible reasons for needing multiple instances come to mind: to
> serve separate templates, to load different macros, or something more
> fundamental e.g. in the configuration.
>
> If you're just serving up differing templates from each instance and
> it's important that they don't cross-over, why not implement your own
> resource loader which limits the template path(s) each request can use
> templates from?
>
> Otherwise, assuming the number of possible Velocity instances is less
> than the number of users you might have, why not create a pool of
> Velocity objects (possibly on-demand?) to fulfill the various needs and
> use the appropriate one each time.
>
> I'm sorry I can't answer your question more directly - I suspect but am
> not certain that there wouldn't be threading issues from storing a
> Velocity instance in the session, but I'd be inclined to step away
> sharply from that course!
>
> - simon
>
>
>
>
> ---------------------------------------------------------------------
> 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
> 


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


RE: Using the session object to store Velocity engines

Posted by Steve O'Hara <so...@pivotal-solutions.co.uk>.
Thanks Simon for your info.  I've got seperate Vlocity instances because I'm
serving up templates from different root directories.
I'm fairly new to Velocity (and Java come to that) so I hadn't considered
delving into the realms of a custom loader.  That would certainly allow me
to run Velocity as a singleton again which would negate the reasons for
storing the instances in the session object.

Thanks

Steve

-----Original Message-----
From: Simon Christian [mailto:simon@stoutstick.com]
Sent: 24 January 2005 15:45
To: Velocity Users List
Subject: Re: Using the session object to store Velocity engines


Hi Steve,

Steve O'Hara wrote:
> Hi,
>
> My Velocity app uses standalone Velocity instances (I've got different
Init
> requirments depending upon the request) - I'm wondering what the effect on
> performance is of putting these instances into the session object rather
> than instantiating them on every request?  Im using Tomcat 4.x
>
> My background is ASP/VB/IIS where storing objects (COM) in the session
> object is a performance no-no because of threading issues in the
container.
> Is this something that Tomcat/Java can do without major penalty?  Has
anyone
> any experience with this sort of thing?
>
> Thanks,
>
> Steve
>

Can I ask why you might need to create a new instance of Velocity per
request (as your message implies)? The instantiation of the Velocity is
a (relatively) slow process compared to actually *using* it.

Three possible reasons for needing multiple instances come to mind: to
serve separate templates, to load different macros, or something more
fundamental e.g. in the configuration.

If you're just serving up differing templates from each instance and
it's important that they don't cross-over, why not implement your own
resource loader which limits the template path(s) each request can use
templates from?

Otherwise, assuming the number of possible Velocity instances is less
than the number of users you might have, why not create a pool of
Velocity objects (possibly on-demand?) to fulfill the various needs and
use the appropriate one each time.

I'm sorry I can't answer your question more directly - I suspect but am
not certain that there wouldn't be threading issues from storing a
Velocity instance in the session, but I'd be inclined to step away
sharply from that course!

- simon




---------------------------------------------------------------------
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: Using the session object to store Velocity engines

Posted by Simon Christian <si...@stoutstick.com>.
Hi Steve,

Steve O'Hara wrote:
> Hi,
> 
> My Velocity app uses standalone Velocity instances (I've got different Init
> requirments depending upon the request) - I'm wondering what the effect on
> performance is of putting these instances into the session object rather
> than instantiating them on every request?  Im using Tomcat 4.x
> 
> My background is ASP/VB/IIS where storing objects (COM) in the session
> object is a performance no-no because of threading issues in the container.
> Is this something that Tomcat/Java can do without major penalty?  Has anyone
> any experience with this sort of thing?
> 
> Thanks,
> 
> Steve
> 

Can I ask why you might need to create a new instance of Velocity per 
request (as your message implies)? The instantiation of the Velocity is 
a (relatively) slow process compared to actually *using* it.

Three possible reasons for needing multiple instances come to mind: to 
serve separate templates, to load different macros, or something more 
fundamental e.g. in the configuration.

If you're just serving up differing templates from each instance and 
it's important that they don't cross-over, why not implement your own 
resource loader which limits the template path(s) each request can use 
templates from?

Otherwise, assuming the number of possible Velocity instances is less 
than the number of users you might have, why not create a pool of 
Velocity objects (possibly on-demand?) to fulfill the various needs and 
use the appropriate one each time.

I'm sorry I can't answer your question more directly - I suspect but am 
not certain that there wouldn't be threading issues from storing a 
Velocity instance in the session, but I'd be inclined to step away 
sharply from that course!

- simon




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


Using the session object to store Velocity engines

Posted by Steve O'Hara <so...@pivotal-solutions.co.uk>.
Hi,

My Velocity app uses standalone Velocity instances (I've got different Init
requirments depending upon the request) - I'm wondering what the effect on
performance is of putting these instances into the session object rather
than instantiating them on every request?  Im using Tomcat 4.x

My background is ASP/VB/IIS where storing objects (COM) in the session
object is a performance no-no because of threading issues in the container.
Is this something that Tomcat/Java can do without major penalty?  Has anyone
any experience with this sort of thing?

Thanks,

Steve






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


Re: Best way to use Velocity

Posted by William Fuller <wi...@sbcglobal.net>.
My pen pal was a she.

Interesting suggestion about forwarding the request, that's essentially how
I am testing with it now.  However, I want to get my POJO's into the
Velocity context so that I can use them in my templates.  I don't really
have a use for the toolbox, so I am presently replacing the
ServletToolboxManager with my stuff, and I am creating a new type of Servlet
based on the ViewServlet.  I'm essentially doing the same thing with
Digester as the XMLToolboxManager and the ServletToolboxManager, but of
course I only have the one context and my datatypes are all strings.  I'm
also loading my XML from Derby.  Response time is a premium for me, so it's
a good thing to trim out redundant layers.  In other words, I'm just
creating a Velocity servlet but I'm basing it on Velocity View.

Thanks

Will


----- Original Message ----- 
From: "Shinobu Kawai Yoshida" <sh...@gmail.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Sunday, January 23, 2005 11:04 PM
Subject: Re: Best way to use Velocity


> Hi Will,
>
> > I had a pen-pal named Shinabu decades ago, the cousin of a friend.
>
> ## What a coincidence!  Was it a he or a she?  My name is more common
> for females.
>
> >  Thank
> > you for your advice.  I decided to press on with Velocity View.  I
examined
> > the source code and it looks like the way to get my context loaded up is
to
> > implement the ToolboxManager interface in a class.  Is that the usual
way
> > this sort of thing is done?
>
> To put tools in the Context, you can use the ready-to-use toolbox.xml.
>  As for the other stuff, I think the normal approach is to put it in
> the request/session in a servlet and forward the request to
> VelocityViewServlet.
>
> Best regards,
> -- Shinobu
>
> --
> Shinobu "Kawai" Yoshida <sh...@gmail.com>
>
> ---------------------------------------------------------------------
> 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: Best way to use Velocity

Posted by Shinobu Kawai Yoshida <sh...@gmail.com>.
Hi Will,

> I had a pen-pal named Shinabu decades ago, the cousin of a friend.

## What a coincidence!  Was it a he or a she?  My name is more common
for females.

>  Thank
> you for your advice.  I decided to press on with Velocity View.  I examined
> the source code and it looks like the way to get my context loaded up is to
> implement the ToolboxManager interface in a class.  Is that the usual way
> this sort of thing is done?

To put tools in the Context, you can use the ready-to-use toolbox.xml.
 As for the other stuff, I think the normal approach is to put it in
the request/session in a servlet and forward the request to
VelocityViewServlet.

Best regards,
-- Shinobu

--
Shinobu "Kawai" Yoshida <sh...@gmail.com>

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


Re: Best way to use Velocity

Posted by William Fuller <wi...@sbcglobal.net>.
Hello Shinabu,
I had a pen-pal named Shinabu decades ago, the cousin of a friend.  Thank
you for your advice.  I decided to press on with Velocity View.  I examined
the source code and it looks like the way to get my context loaded up is to
implement the ToolboxManager interface in a class.  Is that the usual way
this sort of thing is done?

Thank you for all of your help.

Will


----- Original Message ----- 
From: "Shinobu Kawai Yoshida" <sh...@gmail.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Sunday, January 23, 2005 7:34 AM
Subject: Re: Best way to use Velocity


> Hi William,
>
> > I have Derby feeding XML documents into Digester, that creates a bunch
of
> > threads that are pushlet publishers and populates a tree of vectors and
> > properties.  That all works just fine with hand-made HTML files (I got
rid
> > of the JSP from pushlets by using an applet calling JavaScript).  Now I
need
> > to generate the HTML from that same data tree.
> >
> > To do this, I need to simply do a getInstance and that will give me a
> > pointer to the root of the tree.  The data was already loaded by another
> > servlet that runs at startup of Tomcat.
> >
> > My question is, what is the best way to use Velocity in this context?  I
> > need to get the data instance before the template is rendered.  Should I
use
> > VelocityView and derrive a class from that to get my data, or should I
just
> > go straight Velocity and load it with the invoker?  Security isn't a
> > consideration here, because this is an embedded application.
>
> Since you are using XML, you might want to take a look at Anakia and
> DVSL.  I recall seeing a document on using DVSL on websites somewhere.
>    http://jakarta.apache.org/velocity/anakia.html
>    http://jakarta.apache.org/velocity/dvsl/index.html
>
> But you are also using Digester and mapping the XML to POJOs, I
> suspect?  Then, I guess you can use VelocityViewServlet just as well.
>    http://jakarta.apache.org/velocity/tools/view/
>
> Best regards,
> -- Shinobu
>
> --
> Shinobu "Kawai" Yoshida <sh...@gmail.com>
>
> ---------------------------------------------------------------------
> 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: Best way to use Velocity

Posted by Shinobu Kawai Yoshida <sh...@gmail.com>.
Hi William,

> I have Derby feeding XML documents into Digester, that creates a bunch of
> threads that are pushlet publishers and populates a tree of vectors and
> properties.  That all works just fine with hand-made HTML files (I got rid
> of the JSP from pushlets by using an applet calling JavaScript).  Now I need
> to generate the HTML from that same data tree.
> 
> To do this, I need to simply do a getInstance and that will give me a
> pointer to the root of the tree.  The data was already loaded by another
> servlet that runs at startup of Tomcat.
> 
> My question is, what is the best way to use Velocity in this context?  I
> need to get the data instance before the template is rendered.  Should I use
> VelocityView and derrive a class from that to get my data, or should I just
> go straight Velocity and load it with the invoker?  Security isn't a
> consideration here, because this is an embedded application.

Since you are using XML, you might want to take a look at Anakia and
DVSL.  I recall seeing a document on using DVSL on websites somewhere.
   http://jakarta.apache.org/velocity/anakia.html
   http://jakarta.apache.org/velocity/dvsl/index.html

But you are also using Digester and mapping the XML to POJOs, I
suspect?  Then, I guess you can use VelocityViewServlet just as well.
   http://jakarta.apache.org/velocity/tools/view/

Best regards,
-- Shinobu

--
Shinobu "Kawai" Yoshida <sh...@gmail.com>

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


RE: Best way to use Velocity

Posted by Steve O'Hara <so...@pivotal-solutions.co.uk>.
Hi William,

This is off topic but you mentioned you've got a Java Applet that does it's
own 'pull' from an URL and then calls Javascript (LiveConnect I presume).

We've got a similar Applet but we have had to devise another mechanism
because there is a catastrophic flaw in the IE VM that means, if a page is
refreshed at the moment that the applet is also pulling a file, then the
browser freezes forever.
If I remember, the problem also exists with the Sun JVM but isn't quite so
easy to replicate.

We have spent weeks trying to bottom out this problem and found that you
can't - hence we've switched to using IFrames.
I'd be interested to know if you've experienced anything similar - perhaps
your applet solves this problem.... Is it a commercial product?

Steve


-----Original Message-----
From: William Fuller [mailto:williamfuller@sbcglobal.net]
Sent: 23 January 2005 11:21
To: Velocity Users List; Shinobu Kawai Yoshida
Subject: Best way to use Velocity


I have Derby feeding XML documents into Digester, that creates a bunch of
threads that are pushlet publishers and populates a tree of vectors and
properties.  That all works just fine with hand-made HTML files (I got rid
of the JSP from pushlets by using an applet calling JavaScript).  Now I need
to generate the HTML from that same data tree.

To do this, I need to simply do a getInstance and that will give me a
pointer to the root of the tree.  The data was already loaded by another
servlet that runs at startup of Tomcat.

My question is, what is the best way to use Velocity in this context?  I
need to get the data instance before the template is rendered.  Should I use
VelocityView and derrive a class from that to get my data, or should I just
go straight Velocity and load it with the invoker?  Security isn't a
consideration here, because this is an embedded application.

Also, is there a good web site with Velocity examples?  The examples beyond
the trivial examples in the distribution?


---------------------------------------------------------------------
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