Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 100 101 102 103 104 105 < 106 > 107 108 109 110 111 112 .. 166 >> Next

062 "<TD BGCOLOR=’yellow’><B>" +
063 "<CENTER>Description</CENTER></B></TD>" +
064 "<TD BGCOLOR=’yellow’><B>" +
065 "<CENTER>Inventory Amount</CENTER></B></TD></TR>");
066
067 //Load the inventory from the database.
068
069 try
070 {
071
072 Connection con = m_dbsingleton.getConnection();
073 if (con == null)
Listing 32.4 (continued)
290 Item 32
074 {
075 out.println("<B>There are currently database problems. " +
076 "Please see your administrator for details.</B>");
077 return;
078 }
079
080
081 Statement stmt = con.createStatement();
082 ResultSet rs = stmt.executeQuery("select * from Inventory");
083
084 while (rs.next())
085 {
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>";
091 else
092 amtString ="<TD><CENTER>" +
093 amt + "</CENTER></TD>";
094
095 out.println("<TR><TD><CENTER>" + rs.getString("Name") +
096 "</CENTER></TD><TD><CENTER>" +
097 rs.getString("Description") +
098 "</CENTER></TD>" + amtString + "</TR>");
099 }
100 rs.close();
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.");
105
106 //Free the connection!
107 m_dbsingleton.freeConnection( con );
108
109 }
110 catch (Exception e)
111 {
112 out.println("There were errors connecting to the database." +
113 " Please see your systems administrator for details.");
114 e.printStackTrace();
115 }
116
117 }
118
119
120 }
121
122
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();
try
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from Inventory");
//do the rest...
singleton.freeConnection(con);
}
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.*;
Previous << 1 .. 100 101 102 103 104 105 < 106 > 107 108 109 110 111 112 .. 166 >> Next