Setting Up Tomcat

If you want to install Tomcat on your home computer, follow theses steps

  1. Download Tomcat binary from Windows Tomcat Binary
  2. Execute the file after it has downloaded

Testing Tomcat

To test that Tomcat is working, follow these steps. (Please note that the links below only work if Tomcat is installed on your PC, running, and the port is 8080.)

  1. From the Start menu, go to Programs, and then Apache Tomcat Group 4.1
  2. Start Tomcat
  3. Open a browser
  4. Go to the URL http://localhost:8080
  5. Select the link for Servlet Examples
  6. Select the Hello World servlet

Modifying the configuration files

In your Tomcat5 directory in the U: drive, or in the installation directory on your home PC, there is a conf directory that contains the configuration files that govern the default behavior of Tomcat. The two files we will modify in the beginning are server.xml and web.xml.

Modifying server.xml

The only change that needs to be made to server.xml is to allow Tomcat to automatically reload a servlet if its .class file has been modified. Open server.xml in a text editor and look for the closing tag. Add this code BEFORE the </host> tag.

<DefaultContext reloadable="true"  />

Modifying web.xml

The only change to make to the web.xml  file is to uncomment the code for the invoker servlet. The invoker servlet allows Tomcat to execute any servlet that is in a directory. Without the invoker servlet, you would need to add information about every servlet to be run in the web.xml file of that directory. Open web.xml in a text editor, look for the following servlet definition, and uncomment it. If this code is not present, then add an uncommented version of it where all the other servlet definitions are.

  <!--servlet>
    <servlet-name>invoker</servlet-name>
    <servlet-class>org.apache.catalina.servlets.InvokerServlet</servlet-class>
    <init-param>

      <param-name>debug</param-name>
      <param-value>0</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>

  </servlet-->

Look for the following servlet mapping and uncomment it. If this code is not present, then add an uncommented version of it where all the other servlet definitions are.

  <!--
    <servlet mapping>
      <servlet-name>invoker</servlet-name>
      <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>
  -->

Restart Tomcat

After making changes to the configuration files, you will need to stop Tomcat and then start it again.

Modify a Servlet

The default installation has a directory of servlets. The servlets will be in
U:/tomcat/webapps/examples/WEB-INF/classes

Try to modify the HelloWorldExample servlet. Edit the .java file and add some text after the </h1>, but before the closing quote. Compile the file and be sure that the new .class file is in the classes directory. It takes a little while for Tomcat to recognize that the .class file has changed. Access the servlet at http://localhost:8080/examples/servlet/HelloWorldExample. If you don't see the new phrase, wait another 30 seconds and reload it. Eventually you will see the new change. You can look in the command shell window that Tomcat opens when it is running. You can see when the servlet has been reloaded.

Creating a Web Application

Web Applications are directories that have a specific structure and contain some required files.

  1. Create a directory for the web app.
  2. Create a subdirectory of this new dir and name it WEB-INF.
  3. Create a subdirectory of WEB-INF and name it classes.
  4. Create a subdirectory of WEB-INF and name it lib.
  5. Create a text file in the WEB-INF directory and name it web.xml. Place the following in the web.xml file
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
    
                   
    </web-app>
    

All Web Applications have this basic structure.

Adding a Context

The next step is to let the servlet engine know that this application exists by adding a context for it.

  1. Edit server.xml in the conf directory.
  2. Find where you already added the DefaultContext.
  3. After the close of the DefaultContext tag, add the following
    <Context path="/your-webapp-dir" docBase="/full/path/to/your-webapp-dir"
          reloadable="true" crossContext="true" debug="0"/>
    
  4. Restart Tomcat

Testing a Servlet

Now you can create a servlet in the your-webapp-dir directory and execute it through Tomcat. Copy amd paste the following into a new file named HelloServlet.java file and compile it. Place the .class file in the your-webapp-dir/WEB-INF/classes directory.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Simple servlet used to test server.
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages 2nd Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.coreservlets.com/.
 *  &copy; 2003 Marty Hall; may be freely used or adapted.
 */

public class HelloServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String docType =
      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
      "Transitional//EN\">\n";
    out.println(docType +
                "<HTML>\n" +
                "<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1>Hello</H1>\n" +
                "</BODY></HTML>");
  }
}

The URL for accessing this would be
http://localhost:8080/your-webapp-dir/servlet/HelloServlet