JSP vs Velocity what is better?

29,317

Solution 1

Advantages of Velocity:

  • strict separation of view from business logic
  • simple syntax that can be understood by graphic designers

Solution 2

@Vartec: I don't think that the "strict separation of view from business logic" is a velocity feature that is not present in jsp. You can do business logic in jsp (more or less) but it's not recommended at all. But I agree in your point regarding the syntax.

Performance

JSP is compiled to Java, so I don't think that velocity is faster. (have not done benchmarks myself)

Ease of use

For designers: velocity For programmers: (IMHO) jsp, because it's closer to code

Ease of creating reusable components

JSP has lots of components Velocity has no components itself (not component oriented)

Availability of open source 3rd parties

I have seen far more projects using JSP or JSP related technologies than velocity. Maybe because velocity is really low level... :-)

IDE support

There are plenty of tools for jsp. Especially the eclipse jboss plugin/tool suite has a good jsp editor.

Plugins for Velocity are mostly not functional or pretty basic (you get lucky if you have syntax highlighting)

Update If you are looking for a templating engine now, I'd suggest to have a look at thymeleaf. It's comparably lightweight to velocity and can be used just to template some text based templates with a few lines of code or used as a full featured templating engine for example within a webapp.

Solution 3

The below is about Freemarker, but the comparisons are probably still relevant.

At this point in these two technologies' development, it seems like the primary reasons to choose one over the other are:

  1. There is something specific that you need that is in one and not the other
  2. You want to prevent view developers from putting Java scriptlets into JSP pages
  3. Your developers are more comfortable in one than the other

Reasons that don't seem to have as much of an impact:

  1. Speed. There are so many layers in a typical Java EE app that have a much greater impact than the couple of milliseconds more or less that a view renderer might take. In fact, this is probably the last layer I would tackle if my app was performing subpar.
  2. IDE support. JBoss Tools provides a Freemarker editor, and JSP tools are well known.
  3. Syntax. JSP 2 and Freemarker have virtually identical syntax for many basic operations, because of EL and JSTL.

Freemarker Example:

<#list foos as foo>
  <tr>
     <td>${foo.field1}</td>
     <td>${foo.field2}</td>
     <td>
        <#list foo.childObjects as child>
           <#if child.name == 'bar'>
              ${child.value}
           </#if>
        </#list>
     </td>
  </tr>
</#list>

JSP-EL-JSTL Example:

<c:forEach items="${foos}" var="foo">
  <tr>
     <td>${foo.field1}</td>
     <td>${foo.field2}</td>
     <td>
        <c:forEach items="${foo.childObjects}" var="child">
           <c:if test="${child.name == 'bar'}">
              ${child.value}
           </c:if>
        </c:if>
     </td>
  </tr>
</c:forEach>

Solution 4

Velocity or even better FreeMarker. In JSP you cannot have runtime dispatch for pojo hierarchies and everything is statically typed which is a pain. Moreover if you create many JSP2.0 custom tags (say more than 100-150) then your development-deployment cycle will slow down heavily due to inefficiencies of Jasper to resolve dependencies efficiently.

On the other hand JSP has great tool support.

slow JSP compilation references:

http://www.mailinglistarchive.com/[email protected]/msg10786.html

http://marc.info/?l=tomcat-dev&m=119377083422720&w=2

Solution 5

I'll focus on using a template engine, because that is what I have most experience with.

It depends on what you really want to do. Servlets in combination with Velocity (or FreeMarker for that matter) offer a very good seperation of logic and presentation. Templates are harder to test, because you would need to evaluate the template to be able to judge wheter the HTML (or whatever else the output format is) is correct. For JSP this can be done in your IDE of choice.

The big advantage of templates is that you can store these completely outside of your application and even update them while your application is running. This is something that is a little harder to do with JSP, although hot deployment comes pretty close.

Reusable components can be created by using the include functionality of the template engine.

Share:
29,317
flybywire
Author by

flybywire

Updated on October 28, 2020

Comments

  • flybywire
    flybywire over 3 years

    What is better between JSP and velocity in - Performance - Ease of use - Ease of creating reusable components - Availability of open source 3rd parties - IDE support

  • Chii
    Chii over 15 years
    i've always wondered why freemarker is considered better than velocity - to me they seem to do exactly the same thing! I havent used velocity before, but I have seen it in bits. I've used freemarker, and i have to say its pretty easy to use. But i expected velocity to be similar, thus my wondering.
  • cherouvim
    cherouvim over 15 years
    FreeMarker is more advanced and it can go to more low level than Velocity, and with great power comes great responsibility ;) freemarker.org/fmVsVel.html
  • sproketboy
    sproketboy over 14 years
    If you mean Velocity is faster to work with then OK but Velocity is NOT faster than JSP. Velocity merges the html string with the data while JSP sends html directly down the writer. If you add logging times around velocity's mergerTemplate call you'll see what I mean.
  • rustyx
    rustyx almost 12 years
    Either way, PROOF is needed! We need numbers, charts, benchmarks! ;)
  • le0diaz
    le0diaz about 8 years
    Proof of your assertions is required. Also what are the exact aspects for velocity to be better than jsp. Efficiency of what? what do you mean with complicated web pages?,how did you manage to get that 5%. at the moment of writing this comment is really an Irresponsible answer