VelocityEngine is threadsafe. Note that context objects are not, so be sure
to create a new one for each request.
WILL
----- Original Message -----
From: "sunil goyal" <[EMAIL PROTECTED]>
To: <velocity-user@jakarta.apache.org>
Sent: Monday, January 16, 2006 7:05 AM
Subject: VelocityEngine thread-safe
Hello all,
I am using VelocityEngine within a servlet. I want to know weather I
should create instance of VelocityEngine everytime (within doPost()
method) or just initialize the instance once in the init() method of
the servlet.
If VelocityEngine is thread-safe then I can perhaps do the following:
VelocityEngine ve;
public void init(ServletConfig arg0) throws ServletException {
ve = new VelocityEngine();
try {
Properties p = new Properties();
p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, path);
p.setProperty("runtime.log", path + "velocity.log");
ve.init(p);
} catch (Exception ex) {
log.error("Error configuring the VelocityEngine: "
+ ex.getMessage());
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
VelocityContext ctx = new VelocityContext();
// depending on request parameters, a particular template is chosen.
Template template = ve.getTemplate("test.xml");
ctx.put("hello","Hello World");
StringWriter strWriter = new StringWriter();
response.setContentType("text/xml; charset=UTF-8");
// template data and context is merged in a stringbuffer
try {
if (template != null) {
template.merge(ctx, strWriter);
}
} catch (Exception ex) {
}
// http response is sent..
PrintWriter writer = new PrintWriter(new
OutputStreamWriter(response
.getOutputStream(), "UTF8"), true);
writer.println(strWriter);
response.setContentLength(strWriter.getBuffer().length());
}
Is the above code alright? Or do I need to create and initialize
VelocityEngine() within the doPost() method.
Thanks
Regards
Sunil
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]