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.
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;
PrintWriter out = response.getWriter();
out.println("<meta http-equiv=" +
out.println("<meta http-equiv='Content-Type' " +
out.println("<td class='ms-classic4-even'>" +
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 nicelyyou can see its output in Figure 2
(generated from a MySQL databasefor 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 codeit simply outputs the raw data from the database. This highlights the first problemcustom 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 developersparticularly 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.