- Apache Tomcat 7 Essentials
- Tanuj Khare
- 1415字
- 2021-08-20 15:35:11
Configuration of Tomcat 7
Until now, we have discussed the various configuration files of Tomcat 7. Now, the interesting part starts while implementing these in practical, or on live systems.
Before we learn the details of the Tomcat server configuration, let's quickly understand how the web application works from the following steps:
- Whenever you hit the URL (for example, www.abc.com), the browser will contact the DNS server.
- The DNS server will contact the ISP for the required information.
- Once the web server accepts the request from the client browser, it will redirect it to the database server.
- In turn, the database server will retrieve the query and respond it back to the web server.
- The web server then forwards the same response to the client browser, and finally, the client browser will display the content to the user.
That's how the web browser gets the content generated by the web server. The following figure explains the web application functionality and different components, which play a vital role for the application to work:

DataSource configuration
For any web application, the database plays a very vital role as it's the backbone for an enterprise application. For an application to perform well, the correct datasource configuration is necessary at the application layer.
Before moving further, let's quickly discuss how the web application gets the response from the database server.
- Whenever you hit the URL (for example, www.abc.com), the request goes to the web server.
- Once the web server accepts the request from the client browser, it will analyze the request based on the query. If it requires the database (DB) response, then it redirects the request to the database server.
- Based on the query, the database server will retrieve the content and respond to the web server. The web server then forwards the response from the database server to the client browser.
This process flow is also explained in the following figure:

After all the previous discussions, we now understand how the database request flows in the web application. Now, it's time to do the real-time configuration of the datasource of Tomcat 7. Some of the terminologies used in the database connectivity are explained in the following content.
Java Database Connectivity (JDBC) is a Java-based data access technology is an API through which the client accesses the server database. It is oriented towards a relational database and provides a way to query and update the database.
Java Naming and Directory Interface (JNDI) services are an API for the Java platform, which provides naming and directory functionalities to applications written using the Java programming language.
It is a Java object used to access relational databases through the JDBC API. It works well when integrated with the JNDI and after a datasource object is registered with a JNDI naming service. Objects can be accessed by the application itself and connect to the database.
The following are the parameters required for any database server to connect Tomcat 7 with the database and are also the prerequisites for datasource configuration:
- IP address
- Port number
- JNDI name
- Database user ID/password
Note
Database servers in production
The applications which are hosted on the Internet, their web servers are always configured in the Demilitarized Zone (DMZ). For more information on the DMZ zone, please refer to http://en.wikipedia.org/wiki/DMZ_(computing). Database servers are placed in an internal network. In this situation, the firewall port needs to be open between the web servers and the database server for communication.
Database Connection Pool (DBCP) configuration is located in the TOMCAT_HOME
or CATALINA_HOME/lib/tomcat-dbcp.jar
. This specific JAR is responsible for connection pooling. The following screenshot shows the location of tomcat-dbcp.jar
. The following are the built-in properties of the Tomcat 7 server for accomplishing a connection with the database:
- Database Connection pool
- Common DBCP properties

- Configuration of the database server details in
server.xml
- The database specific JAR or JDBC driver needs to be placed in the
lib
directory - The JNDI should be defined in the application
web.xml
file - Application code should have proper JNDI configuration defined
There are many databases available in the market and every DB has its own advantage and disadvantage. We will discuss the most common databases used in the enterprise application and how to configure a datasource for these databases.
DataSource configuration consists of four major steps, irrespective of the database used.
The Oracle database holds a major share in the IT market because of its features. Following are the steps which you need to perform on the datasource configuration of Tomcat.
- By default, the definition of datasource values are defined in the global section of
server.xml
. The following screenshot shows the datasource details inserver.xml:
<!-- Global JNDI resources Documentation at /docs/jndi-resources- howto.html--> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users--> <Resource name="jdbc/tomcat7" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:test" description="test database for tomcat 7" username="admin" password="admin" maxActive="20" maxIdle="10" maxWait="-1"/> </GlobalNamingResources>
- Oracle JDBC driver classes should be placed in the
CATALINA_HOME/lib/
folder of the Tomcat instance. For Oracle, either class12.jar
orojdbc14.jar
is used.Note
By default, Tomcat accepts only
*.jar
. If the driver is in ZIP format, then rename it to.jar
and then deploy it in thejar
directory. Based on the version used in the environment, you can download the Oracle JAR for free using the link, http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html.Tip
In case you have installed the Oracle database version 9i, then you should use the
oracle.jdbc.driver.OracleDriver
class for JDBC connections, and for versions above 9i, you should useoracle.jdbc.OracleDriver
class.oracle.jdbc.driver.OracleDriver
is deprecated and support for this driver will be discontinued from the next major release. - It's always mandatory to define the Document Type Definition (DTD) for the resource in the application
web.xml
. There is always a question that comes to the mind of the administrator, why can't we define the application specific DTD in the serverweb.xml?
The answer to that question is very tricky. When the application is deployed, it will reference the applicationweb.xml
for the resource, but not for the serverweb.xml
. The serverweb.xml
should be used only for the server properties changes, such as the session parameter and so on, which references to the web/application server specific.<resource-ref> <description>Oracle Datasource for tomcat </description> <res-ref-name>jdbc/tomcat7 </res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
- After the previous step, the developer has to reference the JNDI in their code file and connect it to the database.
MySQL is one of the biggest open source databases currently supported by Oracle. It follows the same process as Oracle, but a few parameters vary. The following steps are to be performed to configure the datasource for MySQL:
- The following lines of code provide the definition of datasource in
server.xml
By default, these values are defined in the global section.<Resource name="jdbc/tomcat7" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="tomcatuser" password="tomcat" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/tomcat7"/>
- The following lines of code provide the
web.xml
configuration for the application. This should be placed on theWEB-INF/web.xml
for the application-specific content.<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>Tomcat 7 test DB</description> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/tomcat7</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
- The MySQL JDBC driver is deployed in the
CATALINA_HOME/lib/
folder of Tomcat.MySQL 3.23.47
orConnector/J 3.0.11-stable
are the most common and widely used JAR files.Note
You can download the MySQL JAR freely from the open source website, http://dev.mysql.com/downloads/.
- One of the most important points which the Tomcat administrator should keep in mind is that, in MySQL, the DB should be configured with all privileges for the DB server user. Log in to the MySQL prompt and run the following command to grant the
mysql> GRANT ALL PRIVILEGES ON *.* TO tomcatuser@localhost IDENTIFIED BY 'tomcat7' WITH GRANT OPTION; mysql> create database tomcat7; mysql> use tomcat7; mysql> create table testdata ( id int not null auto_increment primary key,foo varchar(25), bar int);
PostgreSQL is an open source and relational database. It is one of the oldest databases (15 years old). It can be installed on multiple OSes, such as Windows, Unix, MAC, and so on.
It has a four step configuration rule similar to Oracle as follows:
- The following code provides the definition of datasource in
server.xml
. By default, these values are defined in the global section.<Resource name="jdbc/tomcat7" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/tomcat7" username="tomcat7" password="tomcat" maxActive="20" maxIdle="10" maxWait="-1"/>
- The PostgreSQL JDBC driver is deployed in the
CATALINA_HOME/lib/postgresql-9.0-801.jdbc3.jar
folder of Tomcat.Note
Based on the version, the JDBC driver should be downloaded. For more reference on the driver version, refer to http://jdbc.postgresql.org/download.html.
- For the
web.xml
configuration of the application, use the following lines of code. This should be placed in theWEB-INF/web.xml
for the application-specific content.<resource-ref> <description>postgreSQL Tomcat datasource </description> <res-ref-name>jdbc/tomcat7 </res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
At the end of these steps, the developer will reference the JNDI in his/her code file and connect to the database.
- 玩轉智能機器人程小奔
- 傳感器技術實驗教程
- 并行數據挖掘及性能優化:關聯規則與數據相關性分析
- Apache Hive Essentials
- Implementing Oracle API Platform Cloud Service
- Photoshop行業應用基礎
- Silverlight 2完美征程
- The DevOps 2.1 Toolkit:Docker Swarm
- INSTANT VMware vCloud Starter
- 筆記本電腦電路分析與故障診斷
- 電腦上網入門
- PowerMill 2020五軸數控加工編程應用實例
- Photoshop CS4數碼照片處理入門、進階與提高
- 大數據素質讀本
- Flash CS5二維動畫設計與制作