2

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...

ASTX813
  • 313
  • 3
  • 13
  • Just a followup that I didn't want to include in the main question. I don't know how to write the whole table with one call to a servlet because it seems ugly to put all that HTML code into a single String. Or is that just the way it's done, ugly or not? – ASTX813 Oct 04 '10 at 01:30
  • 2
    You're upside down. Unless you're doing ajax, the servlet calls the jsp. The jsp generates the HTML, not the servlet. The job of the jsp is to format data. The job of the servlet is to produce it in a consumable way. – Tony Ennis Oct 04 '10 at 01:32
  • I'm not sure I follow, Tony. Maybe this is totally improper technique, but I'm actually going directly to the JSP in my web browser, and then I have a line like <%=htmlGenerator.writeTable()%> in the BODY. Bad form? – ASTX813 Oct 04 '10 at 02:04
  • Certainly it's bad form. This is maybe how you would do in PHP, but that's a completely different language. – BalusC Oct 04 '10 at 02:08

2 Answers2

6

@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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    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 Oct 04 '10 at 02:07
  • You're welcome. Follow the links at the bottom and I hope that everything will become more clear. – BalusC Oct 04 '10 at 02:09
  • 2
    @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 Oct 04 '10 at 02:10
  • 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 Oct 04 '10 at 02:43
  • @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 Oct 04 '10 at 03:02
5

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
  • 12,000
  • 7
  • 52
  • 73