My client have given me a HTML template for billing and I have to incorporate in my ongoing JSP/Servlet project. It is a raw HTML file and the output should be exactly same with page breaks and so on. It has one header table and one details table like our normal invoice. Any idea how to display the details in the report from Servlet/JSP?
Copy-paste most of it. Then edit it, so that each row (<tr>
) of the table must contain one row from your data, so use:
<table>
<c:forEach items="#{yourData} var="#{row}">
<tr>
<td>#{row.datum1}</td>
<td>#{row.datum2}</td>
</tr>
</c:forEach>
</table>
Just put it in a JSP file and replace everything which needs to be dynamically generated by taglibs and EL. You can use JSP as a template to display HTML (and CSS/JS) the usual way. You can use EL to access "back-end" data (everything which is been put as an attribute in the page
, request
, session
or application
scope) and you can use taglibs to control the page flow dynamically.
As Bozho stated, you can use JSTL (just drop jstl-1.2.jar in webapp's /WEB-INF/lib
) c:forEach
to iterate over a collection of Javabeans. You can use EL to access Javabean properties. You can create a Javabean which represents a Order
(and also Customer
and Item
):
public class Order {
private Long id;
private Date timestamp;
private Customer customer;
private List<Item> items;
// Add/generate public getters and setters.
}
You can use a Servlet
to get a Order
from the database and forward the request to the JSP file for display. E.g.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Long orderId = Long.valueOf(request.getParameter("orderId"));
Order order = orderDAO.find(orderId);
request.setAttribute("order", order);
request.getRequestDispatcher("/WEB-INF/report.jsp").forward(request, response);
}
Map this Servlet
in web.xml
by an url-pattern
so that you can call it by a POST
form or maybe just by a GET
link (replace doPost()
by doGet()
then). You see that the Servlet
puts the found order
in the request scope (so that it is accessible in EL by ${order}
) and forwards the request to a JSP for display. You also see that the JSP is placed in /WEB-INF
to prevent the page from direct access (e.g. by entering JSP's URL in browser address bar).
In the report you can access the Order
like this:
<h1>Order header</h1>
<table>
<tr><th>Order ID:</th><td>${order.id}</td></tr>
<tr><th>Order timestamp:</th><td><fmt:formatDate value="${order.timestamp}" dateStyle="long" /></td></tr>
<tr><th>Customer name:</th><td>${order.customer.name}</td></tr>
<tr><th>Customer address:</th><td>${order.customer.address}</td></tr>
</table>
<h2>Order details</h2>
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<c:forEach items="${order.items}" var="item">
<tr>
<td>${item.name}</td>
<td>${item.description}</td>
<td>${item.quantity}</td>
<td><fmt:formatNumber value="${item.price}" type="currency" currencySymbol="€" /></td>
</tr>
</c:forEach>
</table>
Note that above is just a basic example. You of course need to use your HTML template instead.