Developing Web Applications with the Tomcat Engine The Apache Tomcat Project
Writing a Simple Web Application

Writing a Tomcat Manged Database Application

Install and Start MySql database

Create a Database Table and Populate some Data

Configure Tomcat to maintain a pool of database connections

Create a JSP page to access your table using a Tomcat-provided database connection

Creating a Web Archive

Back to Index


Writing a Simple Web Application

1. Create a new directory for your application, say

MyFirstApp under %DEPLOYMENT_HOME%\apps

2. Let's write our first JSP in MyFirstApp directory, and call it index.jsp.

click here to view index.jsp

3. For Tomcat to recognize this directory as a Web application, we need to
create a subdirectory called WEB_INF that contains web.xml under MyFirstApp.

click here to view web.xml

If you have Tomcat running, you might see a message in the output window 
indicating that Tomcat has noticed this application.  Otherwise, don't worry.

4. Create your application descriptor file in 
%CATALINA_HOME%\conf\Catalina\localhost and call it myfirstapp.xml
This is where you are defining the Context tag for your application.

click here to view MyFirstApp.xml

5. Lets Test our application now. Point your browser to 
http://localhost:8080/MyFirstApp

On the background, 
 your JSP page will be compiled by the JSP engine - Jasper into a Java servlet
 this servlet is handled by the servlet engine - Catalina. Servlet engine then loads the servlet 
class using a class loader and executes it to create dynamic HTML that is sent to the browser
 the servlet creates necessary objects, to present requested data on the browser. 

 


Writing a Tomcat Manged Database Application


Install and Start MySql database

1. Download and install MySQL - open-source database server from 
http://www.mysql.com 2. Install JDBC drivers for MySQL from http://www.mysql.com/downloads/api-jdbc-stable.html Locate the actual jar file - example: mysql-connector-java-3.0.9-stable-bin.jar and copy this file into %CATALINA_HOME%\common\lib folder. 3. Start MySQL Command Line Client. Default login should be admin user, root with no password, unless you created a password during installation. Create a Database Table and Populate some Data mysql> create database family; Query OK, 1 row affected <0.00 sec> mysql> use family; Database changed mysql> create table myfamily ( name varchar(255) not null, relationship varchar(255) not null, id varchar(255) not null); Query OK, 0 rows affected <0.03 sec> mysql> insert into myfamily values("Sid", "father", 1); Query OK, 0 rows affected <0.02 sec> mysql> insert into myfamily values("Vid", "mother", 2); Query OK, 0 rows affected <0.02 sec> mysql> insert into myfamily values("Maa", "child", 3); Query OK, 0 rows affected <0.02 sec> mysql> insert into myfamily values("Aaa", "child", 4); Query OK, 0 rows affected <0.02 sec> mysql> select * from myfamily; You should see the data that you just inserted. Click here to view the SQL Script Configure Tomcat to maintain a pool of Database Connections Create a JNDI resource in Tomcat by defining this in %CATALINA_HOME%\conf\server.xml file or in a seperate file in %CATALINA_HOME%\conf\Catalina\localhost directory. Let's create myFirstDB.xml in $CATALINA_HOME\conf\Catalina\localhost JNDI - Java Naming and Directory Interface, provides a global memory tree to store and lookup configuration objects, and lets applications cleanly separate configuration from the implementation. Click here to view myFirstDB.xml file In myFirstDB.xml file: javax.sql.DataSource resource is created. A DataSource instance, as its name implies, represents a source of data -- such as a database name, the driver to load and use, and its location. resource parameters are defined. Create a JSP page to access your table using a Tomcat-provided Database Connection Now let's write our JSP application that will perform the following tasks: application will do a JNDI lookup on the connection pools that are available in Tomcat, establish a JDBC connection, and retrieve the rows from the table. Creating a Web Archive 1. Open a command prompt window, and goto %DEPLOYMENT_HOME%\apps\MyFristApp directory. 2. Type: C:> jar cvf MyFristApp.war
Page Created by Vidya Sudarsan
Free Guestbook from Bravenet.com Free Guestbook from Bravenet.com