Velocity, qual dos dois?

0 respostas
I
Fala pessoal, tudo bom? Qual a diferença de eu usar assim numa servlet:
public class MeuTesteVelocity extends HttpServlet{
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1L;
    public void init(ServletConfig config) throws ServletException {
        String docRoot = config.getServletContext().getRealPath("");

        Properties p = new Properties();
        p.setProperty("file.resource.loader.path", docRoot);

        try {
            Velocity.init(p);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void service(HttpServletRequest request, 
            HttpServletResponse response) 
            throws IOException, ServletException {
            
            VelocityContext context = new VelocityContext();
            context.put("nome", "Igor");

            Template template;
            try {
                template = Velocity.getTemplate("ola.vm");
                response.setContentType("text/html");
                template.merge(context, response.getWriter());
            } catch (ResourceNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParseErrorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

}
Ou assim:
public class NovoTeste extends VelocityServlet {
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 1L;

    public Template handleRequest(HttpServletRequest request,
            HttpServletResponse response, Context context) {
        context.put("nome", "Alberto Folião");
        Template template = null;
        try {
            // buscando o template
            template = getTemplate("ola.vm");
        } catch (ResourceNotFoundException rnfe) {
            // nao acho o template!
        } catch (ParseErrorException pee) {
            // syntax error no template
        } catch (Exception e) {
            // excecao lancada pelo codigo usado durante a renderizacao/merge
        }
        return template;
    }
}

Valeu, abraços

Criado 10 de fevereiro de 2005
Respostas 0
Participantes 1