Tuesday, March 20, 2007

Manually Deploying A New Tomcat Web Application

Tomcat is the first choice for all students,like me, who have started learning JSP or servlet. But again like me, many students finds it hard to get started with it as there is no in-built tool for deploying a new application in Tomcat. So in this post i am sharing my little experience on starting a new Tomcat web-application.

First of all we have to be familier with the directory structure of tomcat. In my computer Tomcat 5.5 is installed at D:\tomcat5. Under it there are 9 directories.

a) bin - all executables and the main starting point of Tomcat server
b) conf - all configuration files
c) log - all Tomcat log files
d) temp - for temporary use by Tomcat
e) common - all common use compiled java classes
f) server - all classes required to run the Tomcat server
g) shared - all classes shared by Tomcat and any other web-applications
f) work - used by Tomcat as its working directory, all applications are served form this directory
g) webapps - default folder to store web applications

among the above said, we are mainly interested in the last directory i.e. webapps.

To start a new application create a new folder under the webapps directory. Lets say we want start a application named "app1". So create a directory "app1" under "webapps". Then put all the JSP and HTML files under it.

Now at least one more directory need to be created under "webapps" and it has to be named "WEB-INF". The "WEB-INF" directory should contain a XML file named web.xml which is used as the configuration file of our appliocation. Here is a minimum web.xml file.


<?xml version="1.0" encoding="UTF-8"?>
<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">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>

</web-app>

Save this as web.xml under the "WEB-INF" folder. Now we will create a JSP test page.

Save the following lines as "index.jsp" under "app1" directory.


<html>
<head>
<title>Testing Tomcat</title>
</head>
<body>
<% out.println("Hello, World! I am a JSP Page"); %>
</body>
</html>


So, after all this our directory structure should be like this

D:\tomcat5
 - webapps
   - app1
    - index.jsp
    - WEB-INF
     - web.xml

Now start Tomcat and locate our browser to the address "http://localhost:8080/app1/"

Wrintng and deploying a servlet requires a little more work. So to keep things simple i will describe that in another post.

No comments:

Post a Comment