You are on page 1of 9

Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

Global Open Versity


IT Systems Integration Hands-on Labs Training Manual

Connecting Tomcat Application Server to MySQL & Oracle 10g XE


on Linux using JDBC
By Kefa Rabah, krabah@globalopenversity.org April 30, 2009 SerengetiSys Labs

Installing Java SE JDK 6 and Tomcat 6


The aim of this paper is to download and install Java SE JDK 6 and the Tomcat binaries. Next we
show you how to set up Tomcat AS to connect MYSQL and Oracle on a Linux system. Finally, we
will make it a secure web applications server using SSL Certificate.

Part 1: Install JDK6


1. To download Java SE SDK 6, just go to http://java.sun.com/javase/downloads/index.jsp,
and follow the instructions to download a file called: jdk-6-linux-i586-rpm.bin, and save it
in /usr/java directory. You may have to create the java directory if it’s not there.
2. To install Java SE SDK, just type following commands in /usr/java directory.

# chmod 755 jdk-6-linux-i586-rpm.bin


#./jdk-6-linux-i586-rpm.bin

3. To verify that JDK6 is installed in /usr/jav/jdk1.6.0_12 directory, type the following command
to test:

#/usr/java/jdk1.6.0_12/bin/java -version

Part 2: Install Apache Tomcat Web Server


1. To download Tomcat 6, just go to http://tomcat.apache.org/, and follow the instructions to
download a file called: apache-tomcat-6.0.2.tar.gz, and save it in /usr/ directory.

2. Next, we are going to install tomcat in /usr directory. Go to /usr directory, and type
following commands.

# cp /usr/apache-tomcat-6.0.2.tar.gz /usr
# tar -zxvf apache-tomcat-6.0.2.tar.gz

3. The following command creates a symbolic link to the tomcat directory.

# ln –s apache-tomcat-6.0.2 tomcat

4. Insert the following lines inside /etc/profile or /root/.bashrc.

export JAVA_HOME=/usr/java/jdk1.6.0_12
export CATALINA_HOME=/usr/tomcat
export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH

5. Before we begin, we will need to ensure that CATALINA_HOME and JAVA_HOME are correctly set.
To do this, we open a terminal and type the following:

# echo $CATALINA_HOME

# echo $JAVA_HOME
1
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

6. If everything is fine, you can start/stop Tomcat with the following command.

# $CATALINA_HOME/bin/startup.sh

7. To verify that the Tomcat installation is complete, open a Web browser, and type in following
URL: http://<YOUR_TOMCAT _IP>:8080, DNS, or you can also simply use
http://localhost:8080. If everything is fine, you should be able to see a web page such as
one shown below.

8. To stop Tomcat, type:

# $CATALINA_HOME/bin/shutdown.sh

Part 3: Configuring the Manager Web Application


The Tomcat manager is a web application bundled with the default Tomcat installation. It enables
users performing many kinds of operations, such as deploying, undeploying, starting and
stopping a web application, and so on. By default, all access to the Tomcat manager needs to be
authenticated, using a username and password with a role of manager from the file

$CATALINA_HOME/conf/tomcat-users.xml

The following procedure provides the steps for configuring access to the Tomcat manager.

1. Edit the $CATALINA_HOME/conf/tomcat-users.xml file and add the user admin and
password admin. The file should now appear as follows:

<?xml version='1.0' encoding='utf-8'?>


<tomcat-users>
<role rolename="manager"/>
<user username="admin" password="admin123" roles="manager"/>
</tomcat-users>

2
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

2. Start the Tomcat Application server as we did earlier.

Open a browser and enter the following URL:

http://<TOMCAT_SERVER_IP>:8080/manager/html

When prompted by the authentication dialog box, enter the username admin and
password admin123

Part 4: Configuring Virtual Hosting


A virtual host enables more than one website running on a single server to work seamlessly. A
host represents a fully qualified domain name or IP address, such as www.website1.com and
www.website2.com. The Tomcat configuration file server.xml has a default host named
localhost.

The most widely used virtual host is name-based, which means that multiple names are running
on one IP address. Since users provide the hostname as part of the URL, they are not aware that
various websites are using the same IP address.

The following steps illustrate the use of virtual hosting:


1. To configure virtual hosting, you must resolve two host names to the same IP address.
One method is to edit the /etc/hosts file of the client system by adding the following line:

<YOUR_WEB_SERVER_IP> www.website1.com www.website2.com

In our case we have:

192.168.83.176 www.website1.com www.website2.com

2. Verify that both websites are resolved to the same IP address by entering the following
commands:

# ping www.website1.com

# ping www.website2.com

3. Edit the $CATALINA_HOME/conf/server.xml file by locating the Engine portion of the file
and adding the two <HOST> elements. The file should now appear as follows:

<Host name="www.website1.com" appBase="website1apps"


unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false" />

<Host name="www.website2.com" appBase="website2apps"


unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false" />

4. Create the following two directories:

$CATALINA_HOME/website1apps

$CATALINA_HOME/website2apps

5. To copy the ROOT directory from the webapps directory into the new directories you
created in Step 4, enter the following commands:
3
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

# cp –r $CATALINA_HOME/webapps/ROOT $CATALINA_HOME/website1apps

# cp –r $CATALINA_HOME/webapps/ROOT $CATALINA_HOME/website2apps

6. Create the file $CATALINA_HOME/website1apps/ROOT/MyHtml.html using the following


content:

<HTML><HEAD></HEAD>
<BODY DIR="LTR">
<P ALIGN=CENTER STYLE="margin-bottom: 0cm">
<FONT FACE="Times New Roman, serif">
<FONT SIZE=6>Welcome to: www.website1.com</FONT>
</FONT></P></BODY>
</HTML>

7. Create the file $CATALINA_HOME/website2apps/ROOT/MyHtml.html with the following


content:

<HTML><HEAD></HEAD>
<BODY DIR="LTR">
<P ALIGN=CENTER STYLE="margin-bottom: 0cm">
<FONT FACE="Times New Roman, serif">
<FONT SIZE=6>Welcome to: www.website2.com</FONT>
</FONT></P></BODY>
</HTML>

8. Restart Tomcat and verify that the virtual hosting configuration operates properly by
launching a browser and entering the following URL:

http://www.website1.com:8080/MyHtml.html

The following message appears:

Welcome to: www.website1.com

In a second browser, enter the following URL:

http://www.website2.com:8080/MyHtml.html

The following message appears:

Welcome to: www.website2.com

Part 5: Connecting Tomcat to a Database

5.1 Connecting Tomcat and MySQL


MySQL is the leading open source database, and is used widely for web application middleware
environments. MySQL stores relational data and enables fast, simple access and updates to
stored data through the standard JDBC APIs. These APIs are supplied by MySQL in the form of
the Connector/J software. This section describes the steps to connect Tomcat to MySQL.

5.1.1 Obtaining and Installing the JDBC Drivers


Perform the following steps on the Tomcat web server:

1. Download the MySQL JDBC driver 5.0 from the MySQL website:
4
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

http://dev.mysql.com/downloads/connector/j/5.0.html
2. Unzip the downloaded packages by entering the following command:
# tar zxf mysql-connector-java-<version>.tar.gz
3. Install the JDBC driver for Tomcat by entering the following command:

# cp mysql-connector-java-<version>-.jar $CATALINA_HOME/lib

5.1.2 Creating a MySQL Data Source


To create a data source binding for a JDBC driver, perform the following steps on the Tomcat web
server:

1. Create a data source descriptor file named $CATALINA_HOME/conf/context.xml and


insert the following lines:

<Context>

<Resource name="jdbc/mysqlDB" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://<IP_OR_NAME_OF_MYSQL_SERVER>:3306/<DATABASE_NAME>"
username="<DB_USERNAME>"
password="<DB_PASSWORD>" maxActive="10" maxIdle="10" maxWait="-1" />
</Context>

For our example we have:

<Resource name="jdbc/mysqlDB" auth="Container"


type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/osmsdb" username="osmsuser"
password="osmspass" maxActive="10" maxIdle="10" maxWait="-1" />

2. Start the Tomcat web server by entering the following command:

# $CATALINA_HOME/bin/startup.sh

5.1.3 Configuring the MySQL Server


To configure the MySQL database, perform the following steps on the MySQL server:

1. As root user, verify that the MySQL server is running properly by entering the following
command:

#ps -ef | grep mysqld

If the MySQL server is running, a process named mysqld displays in the output;
otherwise, enter the following command:

# /etc/init.d/mysqld start

2. If the database used in the Tomcat data source does not exist, create the database by
entering the following command:

# mysql -u root –p

5
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

mysql> CREATE DATABASE osmsdb DEFAULT CHARACTER SET utf8;


mysql> GRANT ALL PRIVILEGES ON osmsdb.* TO 'osmsuser'@'localhost' \
-> IDENTIFIED BY 'osmspass';
mysql> FLUSH PRIVILEGES;
mysql> QUIT;

3. The name of the database created is in this case osmsdb. We have also created the user
in the Tomcat data source and granted the appropriate privileges. In this case, the user
named osmsuser with the password osmspass is created and is granted all operation
privileges on the database osmsdb.

5.1.4 Testing MySQL Database Connectivity


To test the connectivity between Tomcat and MySQL, perform the following steps on the Tomcat
web server:

1. Create a JSP file named $CATALINA_HOME/webapps/ROOT/Testmysql.jsp and insert the


following lines:

<%@page contentType="text/html" import="java.io.*,java.sql.*,


javax.sql.*,javax.naming.*"%>
<html>
<head>
<title>Tomcat->Tomcat AS with MySQL Test </title>
<link rel="stylesheet" href="style_master.css" type="text/css">
<meta http-equiv="cache-control" content="no-cache">
</head>
<body>
<%
InitialContext ctx = new InitialContext();
DatabaseMetaData dm = null;
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysqlDB");
Connection conn = null;
Statement stmt = null;
try {
conn = ds.getConnection();
dm =conn.getMetaData();
out.println("Connected to-> database version"
+dm.getDatabaseProductVersion());
}catch (Exception sqlex) {
out.println(sqlex.getMessage());
}finally{
conn.close();
}
%>
</body>
</html>

2. If needed, start Tomcat by entering the following command:

# $CATALINA_HOME/bin/startup.sh

3. Verify Tomcat is connecting to MySQL by opening a browser and navigating to the


following website:

http://<TOMCAT_SERVER_IP>:8080/Testmysql.jsp

6
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

The test page opens and the following message is displayed:

Connected to-> databse version5.0.22

5.2 Connecting Tomcat and Oracle


Oracle is the leading commercial relational database. It is generally used to store and manage
sensitive and mission-critical data. As with MySQL, Oracle can also be used as the persistence
layer for web applications. A JDBC connector for Oracle is needed to integrate an external Oracle
Database server with the Tomcat Web server. This section describes the steps for connecting the
Tomcat Web server to an Oracle 10g XE database which is an open source that is free download
and use.

5.2.1 Obtaining and Installing the Oracle JDBC Driver


Perform the following steps from the Tomcat Web server:

1. Download the JDBC driver for Oracle 10g Release 2 from the Oracle website at:

http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc_10201.
html

NOTE: For the Oracle JDBC driver with JDK 1.4 and 1.5 and later, use the ojdbc14.jar
file.

2. Copy the JDBC driver to the Tomcat environment by entering the following command
from the download directory:

# cp ojdbc14.jar $CATALINA_HOME/lib

5.2.2 Creating an Oracle 10g XE Data Source


To create a data source binding for the Oracle JDBC driver data source, perform the following on
the Tomcat Web server:

Create a file named $CATALINA_HOME/conf/context.xml and insert the following lines:

<Context>

<Resource name="jdbc/oracleDB" type="javax.sql.DataSource"
password="<DB_PASSWORD>"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2" maxWait="5000" username="<DB_USERNAME>"
url="jdbc:oracle:thin:@ <IP_OR_NAME_OF_ORACLE_SERVER>:1521:<ORACLE_SID>"
maxActive="4"/>
</Context>

For our example we have:

<Resource name="jdbc/oracleDB" type="javax.sql.DataSource"


password="tomdbpass"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2" maxWait="5000" username="osmsuser"
url="jdbc:oracle:thin:@192.168.176:1521:XE" maxActive="4"/>

Note that with Oracle 10g XE, the database instance XE is the default service.

7
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

5.2.3 Configuring the Oracle Server


Perform the following steps on the Oracle Database server:

1. Verify that the Oracle server and the listener are running by entering the following
command:
# lsnrctl status

2. Create a database user and grant the proper privileges, which should be the same as the
user in the $CATALINA_HOME/conf/context.xml file, by entering the following
commands:

# sqlplussys/<yourpassword> as sysdba
SQL> create user osmsuser identified by osmspass;
SQL> grant connect,resource to osmsuser;

5.2.4 Testing the Oracle Database Connectivity


Test the connectivity between the Tomcat server and the Oracle database by performing the
following steps on the Tomcat Web server:

1. Create a JSP file named $CATALINA_HOME/webapps/ROOT/Testoracle.jsp and insert the


following lines:

<%@page contentType="text/html" import="java.io.*,java.sql.*,


javax.sql.*,javax.naming.*"%>
<html>
<head>
<title>Tomcat AS to Oracle Test </title>
<link rel="stylesheet" href="style_master.css" type="text/css">
<meta http-equiv="cache-control" content="no-cache"></head>
<body>
<%
InitialContext ctx = new InitialContext();
DatabaseMetaData dm = null;
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracleDB");
Connection conn = null;
Statement stmt = null;
try {
conn = ds.getConnection();
dm =conn.getMetaData();
out.println("Connected to-> database version"
+dm.getDatabaseProductVersion());
}catch (Exception sqlex) {
out.println(sqlex.getMessage());
}finally{
conn.close();
}
%>
</body>
</html>

2. If the Tomcat Web server is not running, start it by issuing the following command:

# $CATALINA_HOME/bin/startup.sh

3. Verify that the Oracle test page is displays by opening a browser and navigating to the
following website:
8
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License


Global Open Versity Labs Connecting Tomcat AS to MySQL & Oracle Databases

http://<TOMCAT_SERVER_IP>:8080/Testoracle.jsp

The Oracle Test page appears and the following message is displayed:

Connected to-> database version Oracle Database 10g Express Edition


Release 10.2.0.1.0 – Production.

Hurrah! You are done – have a wonderful Tomcating with MySQL and Oracle 10g XE DBs.

In the follow-update we will look at how include SSL to secure our databases. You may also refer
to SSL on Tomcat HowTo topic on Scribd.com from the same author.

-----------------------------------------------

Kefa Rabah is the Founder and CIO, of Serengeti Systems Group Inc. Kefa is knowledgeable in several fields
of Science & Technology, IT Security Compliance and Project Management, and Renewable Energy Systems.
He is also the founder of Global Open Versity, a place to enhance your educating and career goals using the
latest innovations and technologies.

9
© April 2007, Kefa Rabah, Global Open Versity, Vancouver Canada

www.globalopenversity.org A GOV Open Knowledge Access Technical Academic Publications License

You might also like