velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sunil goyal <sunilgo...@gmail.com>
Subject VelocityEngine thread-safe
Date Mon, 16 Jan 2006 15:05:34 GMT
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: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message