JOT Servlets > JOT Servlets Comparison

JOT Servlets Comparison

There are many server-side programming approaches to generating dynamic web content. This example shows four ways to use Java Servlets to create a list of names on a web page. For more information see this link regarding problems with JSP.

Which approach adds the least programming code to the web page?

 1. Java Servlet (println statements) :

public void listNames(
    HttpServletRequest req,
    HttpServletResponse rsp) throws IOException
{
    PrintWriter out = rsp.getWriter();
    out.println("<b>List of Names</b><p>");
    List namesList =
        (List)req.getAttribute("NamesList");
    Iterator iter = namesList.iterator();
    while(iter.hasNext()){
        Person p = (Person)iter.next();
        out.println(p.getName()+"<br>");
    }
}
HTML embedded in Java: The servlet writes the response with HTML hardcoded in println statements.
 2. JSP Scriptlet (Java) :

<b>List of Names</b><p>
<%
List namesList =
    (List)request.getAttribute("NamesList");
Iterator iter = namesList.iterator();
while(iter.hasNext()){
    Person p = (Person)iter.next();
    out.println(p.getName()+"<br>");
}
%>
Java embedded in HTML: The scriptlet is pure Java code inside a web page between <% and %> tags. The server transforms the JSP page into a Java Servlet.
 3. JSP JSTL (Standard Template Library) :

<b>List of Names</b><p>
<c:forEach items="${NamesList}" var="person">
    <c:out value="${person.name}"/><br>
</c:forEach>
JSP Programming: The JSTL version uses EL (expression language) with an XML programming syntax that includes scoped variables, operators, and object references.
 4. JOT Template :

<b>List of Names</b><p>
JOT.Repeat(JOT.Persons.Next)
    JOT.Persons.name<br>
JOT.End
JOT Tokens in HTML: The JOT version is plain HTML with JOT Tokens, requiring no knowledge of JSP, XML, JSTL, or EL.

© 2007 JOT Object Technologies