velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Will Glass-Husain" <>
Subject Re: VelocityEngine thread-safe
Date Mon, 16 Jan 2006 15:33:53 GMT
VelocityEngine is threadsafe.  Note that context objects are not, so be sure 
to create a new one for each request.


----- Original Message ----- 
From: "sunil goyal" <>
To: <>
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");
                } catch (Exception ex) {
                        log.error("Error configuring the VelocityEngine: "
                                        + ex.getMessage());

        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
                                .getOutputStream(), "UTF8"), true);

Is the above code alright? Or do I need to create and initialize
VelocityEngine() within the doPost() method.



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

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

View raw message