File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Application Frameworks and the fly likes Velocity Templates with Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Velocity Templates with Servlets" Watch "Velocity Templates with Servlets" New topic

Velocity Templates with Servlets

Meenal Srivastva

Joined: Jan 21, 2007
Posts: 19
Hi, im trying to use velocity templates within a servlet get method.
i keep getting the error: Resource not Found for the ".vm" template file.
Below is the code i've used:

VelocityEngine ve = new VelocityEngine();
Properties props = new Properties();
String path = getServletContext().getRealPath("/");
props.setProperty( "resource.loader", "class" );
props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path+"WEB-INF" );


VelocityContext context = new VelocityContext();
context.put("username", "administrator");
Template template = null;

template = Velocity.getTemplate("documentum-config.vm");
/*render the template into a Writer, here a StringWriter */

StringWriter writer = new StringWriter();
template.merge( context, writer );

File file=new File("/WEB-INF/documentum-config.xml");
FileOutputStream fos=new FileOutputStream(file);
DataOutputStream dos=new DataOutputStream(fos);

Any pointers are welcome !

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

First statement tells Velocity to use class loader for getting .vm resources.
Second one tells package name. I doubt that you placed resources under WEB-INF/classes/context_name/WEB-INF/*.vm

Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
I agree. Here's the link:
subject: Velocity Templates with Servlets
It's not a secret anymore!