tags:

views:

140

answers:

2
+1  Q: 

html report in jsp

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?

+1  A: 

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>
Bozho
+2  A: 

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="&euro;" /></td>
        </tr>
    </c:forEach>
</table>

Note that above is just a basic example. You of course need to use your HTML template instead.

BalusC
sorry for being such a naive , what is the difference btwn $ and # in EL @Bozho ,as an up vote is already added to your kitty, i will cast my upvote to Mr.BalusC :-) tomorrow when i finishes this
sansknwoledge
`#{}` notation is the newer/unified EL, usually only used in JSF/Facelets, not in plain vanilla JSP. Bozho is sitting too long/much in JSF world :)
BalusC
one more question stuck with this code got a bean holds the dc header details and also contains list(dc itmes) , now i am struck in populating the bean in servlet , my code in servlet isString dcno=request.getParameter("dcno");CallableStatement cb=con.prepareCall("{call spPrintDC(?)}");cb.setString(1, dcno);ResultSet rs=cb.executeQuery();ArrayList<trnprintdc> datalist=new ArrayList<trnprintdc>();if (! rs.wasNull()){while (rs.next()){// datalist.add()}}the trnprintdc bean has another listof dcdetails in it,the statement list of dcdtls for the number . :-(
sansknwoledge
It's just a basic example. I knew nothing about your datamodel. All you need to do is to replace everything which needs to be dynamically generated/displayed by taglibs and EL. Then just ensure that you've the particular data (it really doesn't matter if it is one bean, a list of beans or even ten loose beans, just let it suit to the datamodel) prepared and ready in the scope with help of a servlet. That's basically all.
BalusC
sorry Mr.Balusc i understand, now i run into a related problem , i am having a error which i made a screen shot here http://www.flickr.com/photos/27616971@N06/sets/72157623272313429/ when trying to access the EL , please guide me.
sansknwoledge
Isn't that just IDE (development tool) specific? Ignore it, run it and see. Else ask a new (and more detailed) question.
BalusC