Advertisment

Web Applications with Tomcat

author-image
PCQ Bureau
New Update

A Web application is a collection of files–HTML, XML, images (GIF, JPEG) and scripts–that reside on an application server (see Application Servers, page 25, PCQuest June 2002) and perform a particular task. If the application is supposed to access a database, the database drivers are included in the Web application. Also included are configuration files (usually in XML) to provide information on the Web application. 

Advertisment

An application server enforces the Web application to be organized in a three-tier architecture: frontend, middleware and backend. It does this by prescribing different directories for the files that present the frontend, files that perform the application logic, files that provide access to the backend (databases) and configuration files. We explain this by taking an example of a simple guestbook Web application. 

Our guestbook Web application consists of a JSP (frontend), a servlet (middleware) and MySQL JDBC drivers (access to backend). The JSP outputs an HTML form where one can fill in his e-mail address, name and comments. The form data after being submitted goes to a Java Servlet (application logic), which appends the form data to a MySQL database (the backend) using the JDBC drivers. Before proceeding further, we assume that you are familiar with Java, JSPs and Servlets. Set up Tomcat as described in Tomcat 4 on PCQLinux 7.1, page 66, PCQuest September 2002. We also assume that you have installed MySQL database on the same machine that runs Tomcat (see Online Databases with MySQL, page 119, PCQuest October 2001). Create a database called guestbook in MySQL and a table named “info” in this database using the following SQL query. 

create table info (name char(20), email char(20), comments varchar(255)) 

Advertisment

Organizing Web application files



The Web applications on Tomcat reside under the directory webapps of the main tomcat directory (/opt/tomcat as per our setup). Create a directory called guestbook under the webapps directory. The guestbook directory is the “root “ directory of our Web application. The files of our Web application will be organized within this directory. Create a subdirectory named “WEB-INF” within guestbook. Create “classes” and “lib” subdirectories with the “WEB-INF” directory. The files of our guestbook Web application will be organized as follows.

Guesbook. It contains all the files that are shown to the user in his browser. This includes all the JSPs (which output HTML), HTML files and images. For our simple guestbook, this directory will contain just one file, namely guestbook.jsp.



guestbook/WEB-INF. This directory contains a configuration file called web.xml 


guestbook/WEB-INF/classes: This directory is the place for the Java class files that perform the application logic. We will place the class file of our Servlet named GuestBookServlet.class in this directory.


guestbook/WEB-INF/lib. For the MySQL JDBC drivers.

The code



The code for guestbook.jsp is as follows.










Please type in the following information:




Advertisment





Name:





Email:





Your comments:




























Notice that for our simple example, this JSP is not outputting any dynamic content and is just a simple HTML file. Place this file in the directory /opt/tomcat/webapps/guestbook. 

Advertisment

The code 



for GuestBookServlet is as follows.

import javax.servlet.*;



import javax.servlet.http.*;


import java.io.*;


import java.sql.*;


public class GuestBookServlet extends HttpServlet


{


public void doPost(HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException 


{


PrintWriter out = response.getWriter();


String name = request.getParameter(“name”);


String email = request.getParameter(“email”);


String comments = request.getParameter(“comments”);


try


{


Class.forName(“com.mysql.jdbc.Driver”).newInstance();


Connection con = DriverManager.getConnection(“jdbc:mysql://127.0.0.1/guestbook?user=root&password=pcq”); 


Statement stmt = con.createStatement();


stmt.executeUpdate(“insert into info values(‘“+name+”’,’”+email+”’,’”+comments+”’)”);


}


catch(Exception e)


{


out.println(e);


}


out.println(“The guestbook data has been submitted to the MySQL database”);


}


}
























Replace “root” and “pcq” in the above code, with the MySQL user and password in your case. Save the above contents in a file named GuestBookServlet.java in the directory/opt/tomcat/webapps/guestbook/WEB-INF/classes. Change to this

directory and compile the Servlet as:

Advertisment

/usr/java/j2sdk1.4.0/bin/javac -classpath /opt/tomcat/common/lib/servlet.jar GuestBookServlet.java

Install JDBC drivers



We have given out Type IV MySQL JDBC drivers called MySQLConnector/J (originally called as MM.MySQL) on this month’s CD, in the directory system/cdrom/unltdoss. Extract the archive, change to the resulting directory mysql-connector-java-21.0.14 and copy the file mysql-connector-java-2.0.14-bin.jar to guestbook/WEB-INF/classes. The Web application, when run, will automatically pick up the drivers from this directory. There is no need to set up any

CLASSPATH. 

Configuration



Type in the following contents in a file called web.xml.

Advertisment

web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd

Save it in the directory guestbook/WEB-INF. Open the file server.xml found in the directory /opt/tomcat/conf and scroll down to the following line.








After this line add the following line which makes Tomcat aware of our guestbook Web application.

Advertisment

reloadable=”true”/>

Running the Web application



Shutdown and restart Tomcat as

/opt/tomcat/bin/shutdown.sh



/opt/tomcat/bin/startup.sh

Now we can access the guestbook web application as http://127.0.0.1:8080/guestbook/guestbook.jsp 

To access the Web application through Apache, open the file httpd.conf, found in /etc/httpd/conf directory. After the line






WebAppDeploy examples conn /examples

add the following line.

WebAppDeploy guestbook conn /guestbook






Restart Apache. 

This was a very simple example of a Web application and have set it up with minimal needed configuration. For more details, see

http://jakarta.apache.org/tom

cat/tomcat-4.1-doc/appdev/index.html
 

Shekhar Govindarajan

Advertisment