velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chachad, Ketan" <>
Subject RE: Using Velocity within a web application but outside of web page generation
Date Fri, 02 Jan 2009 14:16:10 GMT
Hi Steve,

Thank you for the explanation and the intilization code. The same works
for me.

Ketan K. Chachad

-----Original Message-----
From: Steve Cohen [] 
Sent: Friday, January 02, 2009 6:18 PM
To: Velocity Users List
Subject: Re: Using Velocity within a web application but outside of web
page generation

Chachad, Ketan wrote:
> Hi,
> In all the articles I have read for Web Applications, Velocity
> are used for generating web pages.
> The requirement in my web application is to get the velocity template
> and merge it with data retrieved. 
> This merged output is then transmitted for further processing.
> Basically I want to use Velocity within a web application but outside
> web page generation.
> Can anyone provide me with concrete examples or tutorials that would
> help me with this?
> Regards,
> Ketan K. Chachad
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:
This is not so different from what I just did earlier this week. In my 
case, my app was a non-web app that happened for a variety of reasons to

be running inside of Tomcat. My app is an AOL Instant Messenger "bot" 
whose UI is provided via AIM. The "further processing" I needed to do 
was to send it to AIM for eventual retransmission to the end-user. 
Velocity fit very nicely into this scenario. I simply initialized 
Velocity to use the WebappLoader from VelocityTools. (I believe this has

been renamed VelocityResourceLoader in Tools 2.0 if you're going to use 
that version). Velocity can then find the templates at the usual web 
context path.

Anyway here is my initialization code. It is called from the init() 
method of the main servlet that launches my application. This method has

access to the ServletContext that is passed in. BTW 
VelocityInitializationException is my code - it wraps the "throws 
Exception" coming out of the engine.init() call. engine is a 
VelocityEngine object. The class this runs in is a singleton but you 
don't have to do it that way. And the logging is set up to use an 
existing log4j setup but you have other choices there as well.

private void init(ServletContext ctx) throws 
VelocityInitializationException {

this.engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.Log4JLogChute" );


// the following three lines are the key to initializing Velocity to use

the WebappLoader.

this.engine.setProperty("resource.loader", "webapp");

try {
} catch (Exception e) {
throw new VelocityInitializationException(e);
}"following is initialization output from velocity");


To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message