官术网_书友最值得收藏!

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:

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

Configuration of Tomcat 7

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.

  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.

This process flow is also explained in the following figure:

DataSource configuration

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.

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 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
DataSource
  • 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.

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 configuration of Tomcat.

  1. By default, the definition of datasource values are defined in the global section of server.xml. The following screenshot shows the datasource details in server.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>
    
    DataSource for Oracle
  2. Oracle JDBC driver classes should be placed in the CATALINA_HOME/lib/ folder of the Tomcat instance. For Oracle, either class 12.jar or ojdbc14.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 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.

    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 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 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 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 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>
    
  4. After the previous step, the developer has to reference the JNDI in their code file 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 configure the datasource for MySQL:

  1. 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"/>
    
  2. The following lines of code provide the web.xml configuration for the application. This should be placed on the WEB-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>
    
  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 files.

    Note

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

    Tip

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

  1. 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"/>
    
  2. 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.

  3. For the web.xml configuration of the application, use the following lines of code. This should be placed in the WEB-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.

主站蜘蛛池模板: 安塞县| 外汇| 龙游县| 焉耆| 甘肃省| 武陟县| 平安县| 子洲县| 勐海县| 华阴市| 浦县| 光泽县| 当涂县| 郎溪县| 德化县| 常山县| 山西省| 高淳县| 高阳县| 桃园县| 札达县| 炎陵县| 乌拉特前旗| 东港市| 白河县| 阳新县| 河东区| 夏津县| 新源县| 南通市| 和平县| 安阳县| 涟源市| 丰都县| 襄汾县| 望谟县| 金山区| 武夷山市| 福海县| 宿迁市| 锡林郭勒盟|