tags:

views:

96

answers:

3

From my understanding, JSPs are compiled anyway, so I would anticipate you'd get similar performance from both. I want to display a lot of data, and I'm thinking of using JSP for the basics and calling a servlet to generate the code for each row in the table. Unless there's a good way to generate the entire table with one call to the servlet, this would mean several hundred calls, which I imagine is not efficient. What's the "right" way here? Straight servlets would make for a ton of ugly println code, and straight JSP would make for a ton of ugly logic statements...

+5  A: 

The servlet loads up a data structure like a map, puts it into the request, and forwards to a jsp. The jsp iterates and formats. It is very efficient when used for good, not evil.

Tony Ennis
+5  A: 

@Tony is entirely right. Just don't print HTML in the Servlet. This job is for JSP. Also don't write raw Java code in JSP. This job is for Servlet. Once you keep those two simple rules in mind, everything will go well.

Example of Servlet's job:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Product> products = productDAO.list(); // Obtain all products.
    request.setAttribute("products", products); // Store products in request scope.
    request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.
}

Example of JSP's job with little help of JSTL:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
        </tr>
    </c:forEach>
</table>

Simple as that :)

Related questions:

BalusC
Thank you, Balus, that is the most useful information I have come across on this whole topic! It's like you just flipped on a light switch!
ASTX813
You're welcome. Follow the links at the bottom and I hope that everything will become more clear.
BalusC
@ASTX813 - imagine what happens when your boss says he wants the application to work on a Blackberry. Now your servlets, which don't much care about the output device, will change very little. That's the beauty of the separation BalusC has demonstrated.
Tony Ennis
My brain, it hurts. The table is actually outputting the code ${product.name}... etc. Starting to read up on this, but any immediate "oh yeah, you forgot to..." type things? My product object has all private fields, so I'm not sure why product.name would work.
ASTX813
@ASTX813 - convention says that java objects, especially those being used for communication between, say, a servlet and a jsp, should have 'getters' for every field. You don't have to allow the public to set values, but it's helpful if they can _get_ values. If you have a private field called `name` and a public getter called `getName()`, jsp will do the right thing when it sees `someClass.name`. It will actually use reflection to fabricate the `getName()` method and then invoke it.
Tony Ennis
<%@page isELIgnored="false" %> FTW...
ASTX813
A: 

Its all sound like you need a good read on MVC, itself. Please check this thread out.

And for writing scriptless JSPs, JavaRanch Journal just comes handy. Other related posts on JavaRanch Journal, Scriptless JSP Pages: The Power of the Map and Scriptless JSP Pages: The Constant Constants Consternation.

Adeel Ansari