Banner architecture
The architecture for our banner ad servlet is very simple. We will use four classes:
- A general purpose
Logger
class that will write log messages to a text file. - A servlet called
BannerServlet
that will be called every time a banner image is displayed (that is, every time the page loads) and every time a banner image is clicked. This servlet is the heart of our application. - A general purpose
DBHandler
class that ourBannerServlet
will use to communicate with our MySQL database. - A
Banner
class, which we will use to create objects that contain all the metadata we have for each banner in the database.
The BannerServlet
class and the Banner
class are specific to our application. They are fairly straightforward
and you can easily expand them to add more complex features.
The great part about the DBHandler
and Logger
classes are that you can reuse them in practically any application you
write in which you need to communicate with a database or write to a
log file.
We will go into a more detailed discussion about all four of
these classes so you can understand how the servlet works and how it
uses DBHandler
to communicate with our MySQL database.
Logger class
The Logger
class is very simple. It has a single field for the File
object we are logging to. You can pass a reference to a single Logger
object to several classes and have them all write to the same log file. The Logger
class allows you to do several things. You can:
- Create a logger
- Add a divider to the log file (a string of "------"'s)
- Add a log entry by passing in the name of the calling method and the log message
- Add a default message for the start of a method
- Add a default message for the end of a method
- Delete the log file
- Return the
File
object used by the logger
We will use a Logger
object in both the DBHandler
class and the BannerServlet
.
DBHandler class
DBHandler
is a very versatile class and can be used to
interface with just about any database through JDBC. It requires a
string with the name for the JDBC/ODBC driver that we are using to
connect to our database, a string with the name of the database for
which we set up the DSN, and a Logger
parameter. The Logger
parameter tells DBHandler
where to print output messages while working its magic. The constructor for DBHandler
opens a connection to the database. When you are done using DBHandler
, you must close it using the close()
method.
After you create a DBHandler
object, you must create a query to execute. Use the setQueryString()
method to pass in a string with your query in it, which can be in the form of a PreparedStatement
class.
A PreparedStatement
is a great feature of JDBC. It
allows you to define a query string, using question mark characters in
place of variable criteria in the query. You can then use setter
methods on the PreparedStatement
class to set the values of the unknown elements in the query. Fortunately, the DBHandler
class will take care of all of this for us. We just set the query we want and call one of the DBHandler
methods, as shown below:
|
You can perform SELECT
queries using the lookup()
method, UPDATE
queries using the executeUpdate()
method, and INSERT
queries using the insert()
method. There is also an execute()
method that takes no parameters and executes any query that doesn't have any PrepareStatement
arguments.
Banner class
The Banner
class is simply a bunch of setter
and getter
methods that directly correspond to the column values in the ADS database table.
BannerServlet class
The BannerServlet
is the heart of our application. We will break down the different parts
of this class for you. By browsing through the code, you will become
more familiar with how the DBHandler
class is used to connect to the database.
Fields
The BannerServlet
uses five fields:
- String _databaseUrl: The name of the database that we will access (
jdbc:odbc:\\localhost\BANNER
). - String _driverName: The name of the driver that we will use
to talk to the database. As described above, we will use the MM MySQL
JDBC driver. The name of the driver is
org.gjt.mm.mysql.Driver
. - Logger _logger: The name of the
Logger
class we will use to log all events that occur in our application. - HashMap _banners: A HashMap of all the
Banner
objects. This HashMap will be populated in theinit()
method of our servlet. Each row in our database table will translate to a singleBanner
object that is stored in our HashMap. We'll elaborate on this in a moment. - int _totalWeight: The sum of all the
Banner
weights. This value is also set in theinit()
method; we'll discuss this shortly.
init()
The init(ServletConfig)
method
of any servlet is called when the servlet is first loaded by the
container. The container in our case is Tomcat. Tomcat generates and
passes in a ServletConfig
object that contains both
default configuration information that the container sets and custom
configuration information that the developer (you) can set up in the
config files for the servlet. For our purposes, we will not need to
pass in any configuration information, but you may want to expand the
servlet at some point and use this functionality.
The first thing we do in init()
after calling super.init()
is initialize our HashMap variable _banners
and set our _totalWeight
to 0. Then, we connect to the database and get all the rows from our ADS table in the form of a ResultSet
. We iterate through our ResultSet
using a for
loop, construct a Banner
object out of each row, then add the Banner
object into the HashMap using the index of our for-loop as the hash
value. (We could have just as easily used a Vector or some other Collection
class to accomplish the same thing.)
We now have a HashMap of all of our Banner
s in memory. If we update our database, we can just call the init()
method to reload our HashMap again. We will use this in our increaseImpressions()
and decreaseClicksRemaining()
methods.
service()
The service()
method is defined in the HttpServlet
class that our BannerServlet
extends and can process any request, whether it be a GET
or POST
method. Our implementation of the service()
method has two core parts. The first part handles what the servlet does
when the Web page sends an image request, and the second part handles a
link request.
Finally, we look at the type
parameter sent in from the client. If the value of type
was image
, we get a random Banner
object from the database, add the Banner
object to the user's session, increase the number of impressions for
the given banner, and route the user to the image referenced in the
image field of the Banner
object.
If the value of type
was link
, we get the Banner
object off of the session, decrease the clicks remaining on the banner,
and redirect the user to the link specified in the URL field of the Banner
object.
Other methods
getRandomBanner()
, increaseImpressions()
, and decreaseClicksRemaining()
are all helper methods called from the service
method. getRandomBanner()
uses a simple algorithm to select a random banner from our _banners
HashMap. Both increaseImpressions()
and decreaseClicksRemaining()
use DBHandler
to connect to the database and update information for the given Banner
. At the end of these two methods, we call the init()
method to reload the updated Banner
information into our HashMap.
View Generate dynamic content with Tomcat and MySQL Discussion
Page: 1 2 3 4 5 Next Page: Conclusion & Resources