Download (direct link):
062 "<TD BGCOLOR=’yellow’><B>" +
063 "<CENTER>Description</CENTER></B></TD>" +
064 "<TD BGCOLOR=’yellow’><B>" +
065 "<CENTER>Inventory Amount</CENTER></B></TD></TR>");
067 //Load the inventory from the database.
072 Connection con = m_dbsingleton.getConnection();
073 if (con == null)
Listing 32.4 (continued)
290 Item 32
075 out.println("<B>There are currently database problems. " +
076 "Please see your administrator for details.</B>");
081 Statement stmt = con.createStatement();
082 ResultSet rs = stmt.executeQuery("select * from Inventory");
084 while (rs.next())
086 String amtString = "";
087 int amt = rs.getInt("Amount");
088 if (amt < 50)
089 amtString ="<TD><CENTER><FONT COLOR='RED'>" +
090 amt + "</FONT></CENTER></TD>";
092 amtString ="<TD><CENTER>" +
093 amt + "</CENTER></TD>";
095 out.println("<TR><TD><CENTER>" + rs.getString("Name") +
096 "</CENTER></TD><TD><CENTER>" +
097 rs.getString("Description") +
098 "</CENTER></TD>" + amtString + "</TR>");
101 out.println("</TABLE><HR>Items in <FONT COLOR='red'>RED</FONT>"
102 + " denote a possible low inventory. Click Here to " +
103 " contact <A HREF='mailto:mgmt@localhost'>" +
104 "MANAGEMENT</A> to order more supplies.");
106 //Free the connection!
107 m_dbsingleton.freeConnection( con );
110 catch (Exception e)
112 out.println("There were errors connecting to the database." +
113 " Please see your systems administrator for details.");
Listing 32.4 (continued)
Attempting to Use Both Output Mechanisms in Servlets 291
Using this Singleton class, you would simply need to do the following in your servlet:
LavenderDBSingleton singleton = LavenderDBSingleton.getInstance();
Connection con = singleton.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Inventory");
//do the rest...
catch (Exception e)
If the other servlets on our Web server VM use the Singleton in this manner, we will maximize the efficiency of our servlet, reducing the overhead of creating connections for every client (and for every thread!). The initial performance overhead may be the initial instantiation of the Singleton. For that purpose, it may be wise to instantiate it in the servlet's init() method. For the purposes of brevity, we have not included a full program example of this in the book. The Web site, however, will have a code listing that you can use.
In this pitfall, we discussed a few of the performance pitfalls that can arise when servlets communicate directly to a database. We presented two methods of connection pooling with servlets—one where a servlet shares a connection pool with its clients and one where all servlets share a connection pool by calling a Singleton class that will reside in memory in the virtual machine.
It should be noted, however, that there are ways of abstracting the database connection away from the user interface (servlet) model. Using servlets as the front end in a J2EE architecture where Enterprise JavaBeans (EJBs) worry about database connections is a good way for accomplishing this abstraction. While this pitfall was meant for developers who build applications where servlets connect to the database, there will be other pitfalls in this book that will discuss the use of EJBs.
For more information about other methods of connection pooling, see Item 45 in Part Three, "The Enterprise Tier."
Item 33: Attempting to Use Both Output Mechanisms in Servlets
If you've done a lot of servlet programming, you probably recognize this pitfall. The Servlet API provides two mechanisms for printing out a response: PrintWriter and ServletOutputstream. This pitfall discusses problems that may occur in using these two objects and will demonstrate an example.
In Listing 33.1, we have created a simple servlet that takes a quick voting poll on the Internet. We use this servlet along with a helper object called VoterApp, which has
292 Item 33
methods that tally votes, create an HTML-formatted "Poll of the Day," and create a graphical image representing the current tally of today's votes. Our servlet either shows the HTML Poll and creates a form for the user to vote or it shows the user a graph of the current tally. If the vote parameter in our servlet is null, it will create the poll, as shown in Listing 33.1 on line 29. In lines 33 to 40 of our servlet's doGet() method, we create an HTML form, with most of the contents returned from the getPollOfTheDay() method on the VoterApp object.
01 package org 3 3 m e t i s l l a LM t i p a v a j
02 import java .io.*;
03 import java .text.*;
04 import java .util.*;
05 import javax.servlet.*;
06 import javax.servlet.http.*;