The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

Tomcat - Configuration of Tomcat 7


Until now, we have discussed the various confi guration fi les 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 confi guration, let's quickly understand how the web application works from the following steps:

1. Whenever you hit the URL (for example, www.abc.com), the browser will contact the DNS server.
2. The DNS server will contact the ISP for the required information.
3. Once the web server accepts the request from the client browser, it will redirect it to the database server.
4. In turn, the database server will retrieve the query and respond it back to the web server.
5. The web server then forwards the same response to the client browser, and fi nally, 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 fi gure 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 confi guration is necessary at the application layer.

Before moving further, let's quickly discuss how the web application gets the response from the database server.

1. Whenever you hit the URL (for example, www.abc.com), the request goes to the web server.
2. 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.
3. 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.

After all the previous discussions, we now understand how the database request fl ows in the web application. Now, it's time to do the real-time confi guration of the datasource of Tomcat 7. Some of the terminologies used in the database connectivity are explained in the following content.

JDBC

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.

JNDI

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.

DataSource

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 confi guration:
  • IP address
  • Port number
  • JNDI name
  • Database user ID/password
Database servers in production The applications which are hosted on the Internet, their web servers are always confi gured 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 fi rewall port needs to be open between the web servers and the database server for communication.

Database Connection Pool (DBCP) confi guration is located in the TOMCAT_HOME or CATALINA_HOME/lib/tomcat-dbcp.jar. This specifi c JAR is responsible for connection pooling. The following screenshot shows the location of tomcatdbcp. 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
  • Confi guration of the database server details in server.xml
  • The database specifi c JAR or JDBC driver needs to be placed in the lib directory
  • The JNDI should be defi ned in the application web.xml fi le
  • Application code should have proper JNDI confi guration defi ned
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 confi gure a datasource for these databases.

DataSource confi guration consists of four major steps, irrespective of the database used.

DataSource for Oracle

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 confi guration of Tomcat.

1. By default, the defi nition of datasource values are defi ned in the global section of server.xml. The following screenshot shows the datasource details in server.xml:

<!-- Global JNDI resources Documentation at /docs/jndi-resourceshowto.
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>

2. Oracle JDBC driver classes should be placed in the CATALINA_HOME/lib/ folder of the Tomcat instance. For Oracle, eit her class 12.jar or ojdbc14.jar is used.

By default, Tomcat accepts only *.jar. If the driver is in ZIP format, then rename it to .jar and then deploy it in the jar 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.

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 use oracle.jdbc. OracleDriver class. oracle.jdbc.driver.OracleDriver is deprecated and support for this driver will be discontinued from the next major release.

3. It's always mandatory to defi ne the Document Type Defi nition (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 defi ne the application specifi c DTD in the server web.xml? The answer to that question is very tricky. When the application is deployed, it will reference the application web.xml for the resource, but not for the server web.xml. The server web.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 specifi c.

<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>

4. After the previous step, the developer has to reference the JNDI in their code fi le and connect it to the database.

DataSource for MySQL

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 confi gure the datasource for MySQL:

1. The following lines of code provide the defi nition of datasource in server. xml By default, these values are defi ned 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"/>

2. The following lines of code provide the web.xml confi guration for the application. This should be placed on the WEB-INF/web.xml for the application-specifi c 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>

3. The MySQL JDBC driver is deployed in the CATALINA_HOME/lib/ folder of Tomcat. MySQL 3.23.47 or Connector/J 3.0.11-stable are the most common and widely used JAR fi les.

You can download the MySQL JAR freely from the open source website, http://dev.mysql.com/downloads/.

4. One of the most important points which the Tomcat administrator should keep in mind is that, in MySQL, the DB should be confi gured with all privileges for the DB server user. Log in to the MySQL prompt and run the following command to grant the privileges:

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);

If you create the MySQL user without password, then the JDBC driver will fail to connect and you will have an authentication error in catalina.out.

DataSource for PostgreSQL

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 confi guration rule similar to Oracle as follows:

1. The following code provides the defi nition of datasource in server.xml. By default, these values are defi ned 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"/>

2. The PostgreSQL JDBC driver is deployed in the CATALINA_HOME/lib/ postgresql-9.0-801.jdbc3.jar folder of Tomcat.

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.

3. For the web.xml confi guration of the application, use the following lines of code. This should be placed in the WEB-INF/web.xml for the application specifi c 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 fi le and connect to the database.
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com