Setting up Tomcat and MySQL
In this section, we will walk through the setup for both Tomcat and MySQL. We'll then show you how to install the driver you need to enable these two applications to communicate.
Setting up Tomcat
Download and install Tomcat. For this article, we used Tomcat 4.1 for Windows, which comes with a nice install package and creates icons and a Start menu folder for you. It also creates a Windows Service to start and stop the Tomcat server. Installation should be very straightforward, but if you have trouble, refer to the Tomcat documentation. Because of the popularity of Tomcat, there is also ample help available on newsgroups and on the Web, some of which we've listed in Resources.
After you install Tomcat, there are a few steps you need to complete to
set up our rotating banner Web application. First, we will create a
subdirectory called banner
under the [installdir]\webapps
directory. Under the banner
subdirectory, we will then create the standard Web application directory structure:
|
Next, we'll add a context
to point to our Web application. A context is merely an alias, telling
Tomcat where to access our Web application. Our context path will be /banner
, and it will point to the banner
subdirectory we just created. When a user enters http://localhost:8080/banner
, he will be taken to our top-level banner directory under webapps
. If he wants to run our BannerServlet
, which will exist in the WEB-INF/classes
directory, he would use http://localhost:8080/banner/servlet/BannerServlet
.
To add the /banner
context, we first need to edit the server.xml file in the Tomcat conf
directory. Go nearly to the bottom of the file where you will see several context tags. There should be one for /admin
and one for /examples
. Add the following context tag:
|
After you've added the context tag, restart Tomcat so that the changes to the server.xml file take effect (in our case, we just restarted the Windows Service that Tomcat installed).
Setting up MySQL
MySQL is a powerful database for the price you pay for it (nothing), and many companies use MySQL to handle their data. The number is growing daily as companies with low budgets enter the Web market. The open source community has greeted MySQL with open arms. The documentation about this powerful database is abundant, and there are both Linux and Windows versions.
Download and install MySQL
with the "Typical" setup option (for this article, we'll assume you are
working with the WinNT version of MySQL). After you've completed the
installation, you will notice one of the annoying aspects of MySQL: it
doesn't place anything in your Start menu. You will need to go to the
directory where you installed the database (c:\mysql\
, by default), then go to the bin
directory, where you will find the executables to run MySQL.
First, double-click the winmysqladmin.exe file. The first time you open
this file, you will be asked to enter a username and password. Next,
right-click on the street light icon that will appear in your taskbar.
Go to WinNT
and choose "Start the service" to keep MySQL running in the background.
Finally, double-click the "mysql.exe" icon to start "MySQL Monitor,"
where you will work with MySQL.
Getting MySQL and Tomcat to work together
Getting MySQL and Tomcat to communicate with one another can be difficult. With the JDBC API, however, we'll be able to use SQL to talk to a MySQL database from our Java classes with relative ease.
We'll use the MM MySQL JDBC driver, an open source driver, to facilitate communication between MySQL and Tomcat. (At the time of this writing, 2.0.14 is the latest version.)
Unfortunately, setting up this driver is a little tricky. First, download the appropriate JAR file for the driver from here. We downloaded the file called mm.mysql-2.0.14-you-must-unjar-me.jar
.
Next, unjar (or unzip) the file to a temporary directory. Finally, copy
the file that contains the driver from the unzipped directory structure
into your WEBAPPS/BANNER/WEB-INF/lib
directory and restart Tomcat. In the version of the driver we downloaded, the file is called mm.mysql-2.0.14-bin.jar
.
We could have used the JDBC/ODBC bridge driver to communicate with MySQL, but we've assumed that a native driver would provide more of a performance advantage (though we haven't run any benchmarks to prove our assumption). For this application, it probably wouldn't make much of a difference in performance, but we decided to demonstrate how to use the native JDBC driver so you wouldn't have to figure it out when you are designing a larger application.
View Generate dynamic content with Tomcat and MySQL Discussion
Page: 1 2 3 4 5 Next Page: The rotating banner application