Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Rapid Template-Driven Development with Jakarta Velocity : Page 2

Increase your efficiency and lower your stress in developing Web applications by using templates.


advertisement
Approach 1. Using a Servlet
Once upon a time, servlets were (along with CGI) the most popular way to generate content. They're still compelling, when you consider all the freebies that come along with using an application server running a servlet, such as load balancing, clustering, etc., but is very ungainly when it comes to change control.

The download contains a servlet called SalesReport.java that contains the "traditional" server methodology in a method called doServletWithStandard. The following code snippet shows a portion of the method.

private void doServletWithStandard(HttpServletRequest request, HttpServletResponse response) throws IOException { Statement stmt = null; ResultSet rs = null; String strSql = ""; int nColCount = 0; response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println( "<head><title>SalesReport</title></head>"); out.println("<head>"); out.println("<meta http-equiv=" + "'Content-Language' content='en-us'>"); out.println("<meta http-equiv='Content-Type' " + "content='text/html; charset=windows-1252'>"); . . . out.println( "<td class='ms-classic4-top'>Amount</td>"); out.println("</tr>"); while(rs.next()) { out.println(" <tr>"); for(int i=1;i<=nColCount;i++) { out.println("<td class='ms-classic4-even'>" + rs.getObject(i) +</td>"); } out.println(" </tr>"); . . . out.println("</table></body></html>"); out.close();

You can see that this code is full of out.println statements that output the HTML from the mockup, and in the "meat" of the page that presents the list of sales report line items, a loop iterates through a recordset writing out the items embedded within the <td> HTML classes. The servlet works nicely—you can see its output in Figure 2 (generated from a MySQL database—for which the create scripts are available in the download).

Figure 2. The Sales Report Generated by a Servlet: The servlet code generates the report table, using "out.println" statements to integrate the data into the mockup HTML.
The servlet result is pretty close to the mockup; only the "Amount" information is off due to the fact that no formatting was done on the item in the code—it simply outputs the raw data from the database. This highlights the first problem—custom formatting of data, where the value 1200000 should be presented as $1,200,000, in bold print, and right-aligned. To fix the problem, the "Amount" style could be specified in the HTML as a CSS style, and that style applied to the data, or the servlet could output custom HTML containing the formatting information for that column. The point is that small snags like this can add up to many problems in managing change control. Generally, projects like this one have a high friction rate between the business and the developers—particularly when changes are necessary. How many times have you heard a question along the lines of: "It's easy for me to change it in FrontPage, why it is so hard for you to change it in your servlet?" This friction is made even worse when you understand how some tools (such as FrontPage) make changes to the HTML code underlying a page when a change is made (for example) to some of the CSS style definitions.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap