0

I need to dynamically generate html table and insert in the jsp page and would like to know the best way to do this. Theses are the options I am debating over:

  • Generate a string which contains html tags in Servlet and set in attribute to forward to the jsp page
  • Create object which contains data to generate table and set in the attribute to forward to jsp page, jsp page will use tag library to generate the table
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
powpow
  • 147
  • 1
  • 2
  • 8

2 Answers2

2

The latter. Servlets should not know or care about presentation logic. JSTL is usually enough to render the model from within the JSP, but if greater complexity is necessary, use a custom taglib to render the data.

skaffman
  • 398,947
  • 96
  • 818
  • 769
2

The second option is the best one. A simple example:

<table>
<!-- some headings here -->
<c:forEach items="${tableRows}" var="row">
   <tr>
      <td>${row.foo}</td>
      <td>${row.bar}</td>
   </tr>
</c:forEach>
</table>

The servlet should not be aware of HTML

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140