Dummies Guide to Installing and Configuring Tomcat
16 January 2009
Deploying Web Applications Using Tomcat
Definition of a Web Application
Web applications can contain the following items: Servlets JavaServer Pages Utility Classes Static Documents including XHTML, images, HTML etc. Client side classes Meta information that describes the web application.
Directory Structure
The first step to create a web application is to create the directory structure. The directory structure should be created in the
. Eg. TOMCAT_HOME\webapps TOMCAT_HOME – being the installation directory of Tomcat. For a web application called helloworld the following directory structure needs to be created.
Directory /helloworld /helloworld/WEB-INF Contains Root directory for web application contains JSP, HTML etc. Directory contains all resources related to the application that are not in the document root. This is where the web application deployment descriptor is located. Files in this directory cannot be served directory to the client. Servlet and utility classes are placed. Java Archive (jar) files that the web application requires.
/helloworld/WEB-INF/classes /helloworld/WEB-INF/lib
Web Application Deployment Descriptor
The deployment descriptor is the heart of a web application. It is an xml file located in the following directory. \application_name\WEB-INF directory. The deployment descriptor file is called web.xml. A simple web.xml file will look like:
Page No: 1 of 4
Dummies Guide to Installing and Configuring Tomcat
16 January 2009
HelloWorld HelloWorldServlet 1
Web.xml Elements
Element Tag Description Contains the web-application Defines the servlets for your web-application. Each servlet in the web-application should have a entry A canonical name of the deployed servlet The fully qualified class name of the servlet The order in which each servlet should be loaded.
NB: The section at the top of the web.xml file tells Tomcat about how to parse the xml file. It should be included in the file.
Servlet Context
The servlet context defines a set of methods that are used by the components of a web application to communicate within the servlet container. The servlet context acts a as a container for the web application. There is only one servlet context per web application. To let Tomcat know that there is a new web application you need to add the flowing entry into the TOMCAT_HOME\conf\server.xml file to set the values for the path and docbase to the name of your web application.
path=”/helloworld” tell the servlet container that all requests with /helloworld appended to the servers URL belong to the helloworld application. And docbase=”helloworld” tells the servlet container that the web application exists in the /helloworld directory. NB – don’t be too concerned with servlet containers at the moment. Just know that you have to follow the above steps.
Page No: 2 of 4
Dummies Guide to Installing and Configuring Tomcat
16 January 2009
Tomcat Web Application Example – HelloWorld
The following is a listing for the helloworld web application. Try this exercise with your Tomcat server.
Index.html
Hello World Web Application Hello World Part 1
Go to Hello World Servlet
Nextpage.html
Hello World Web Application Hello World Part 3
This is the third page in the HelloWorld web application.
HOME
HelloWorldServlet.java
/** * Class: HelloWorldServlet * Purpose: An example servlet to show how Tomcat works. Is * called from Index.hmtl, * has a link to another html page - nextpage.html. * * Author: Karen Keefe * Date: 15 September 2002 * Version: 1.0 */ import java.io.* ; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html") ; PrintWriter out = response.getWriter() ;
Page No: 3 of 4
Dummies Guide to Installing and Configuring Tomcat
16 January 2009
out.println("") ; out.println("") ; out.println("") ; out.println("Hello World Web Application") ; out.println("") ; out.println("") ; out.println("") ; out.println("Hello World Part 2
") ; out.println("
") ; out.println("This is the HelloWorldServlet") ; out.println("
") ; out.println("This link here will take you to another page.") ; out.println("") ; } }
Web.xml
HelloWorld HelloWorldServlet
File Locations
\helloworld\index.html \helloworld\nextpage.html \helloworld\WEB-INF\web.xml \helloworld\WEB-INF\classes\HelloWorldServlet
References
Goodwill, James Apache Jakarta-Tomcat Apress 2002
Page No: 4 of 4