Programming on the Web
 

Tomcat and Java Servlets


Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are developed by Sun under the Java Community Process.

Status: Installed with user privileges in

/u/csc309h/lib/jakarta-tomcat-5.0.28

To assist you in getting your server up and running there are a few files provide here. Copy the files to a directory somewhere under your home directory from which you will be running your tomcat server. In the rest of this document, we will refer to this directory as $CATALINA_HOME.

To get your server running:

  • cd to $CATALINA_HOME/bin
  • Run the script start.sh (it is important that you run this file from within the bin directory)
  • Enter the port number you were assigned for running Tomcat. Make sure you only use your assigned port number. Tomcat will use 3 ports, your assigned number and the next two (e.g., if you are assigned port 32000, tomcat will use 32000, 32001, 32002).
  • The start.sh script will create the configuration file conf/server.xml.

To check if your web server is running, issue the command (on CDF):

/bin/ps -ef | grep your_user_id

You can now point your browser at http://localhost:your_port_number and you will see the default tomcat page displayed.

To stop your server, run the bin/stop.sh script. Always remember to stop your server before logging off CDF.

You can look through the example servlet pages provided by tomcat.

Some servlet resources:

Installing and Compiling Servlets

Download the sample application from here.

Decompress and untar the file. Then copy the csc309 directory to $CATALINA_HOME/webapps, where $CATALINA_HOME is an environment variable that points to the directory where you have installed Tomcat (the directory that contains bin, conf, and webapps).

The sample application consists of a single servlet implemented by the HelloWorld class

To run the servlet follow these steps:
  1. Compile the Java class
    • Include the following jar file in your CLASSPATH /u/csc309h/lib/tomcat-5.0.28/common/lib/servlet-api.jar.
      This library contains class definitions commonly used by servlets, such as the HTTPServlet class.
    • Alternatively, add the commands in this link to your .cshrc file in your home directory. For the commands to take effect you will need to either login again or force your shell to reread the configuration file by typing source .cshrc.
    • cd to $CATALINA_HOME/webapps/csc309/WEB-INF/classes
    • Compile the servlet by typing:
      javac HelloWorld.java
  2. Start Tomcat
  3. Browse to the following URL
    http://127.0.0.1:yourPortNumber/csc309/servlet/HelloWorld

Adding a Servlet to a Web Application

To add the PrintEnv servlet to the csc309 Web Application follow these steps:
  1. Copy PrintEnv.java to $CATALINA_HOME/webapps/csc309/WEB-INF/classes
  2. Compile PrintEnv.java
  3. Add the following entries to the application descriptor located at $CATALINA_HOME/webapps/csc309/WEB-INF/web.xml.
       <servlet> 
            <servlet-name>PrintEnv</servlet-name> 
            <servlet-class>PrintEnv</servlet-class> 
       </servlet> 
       <servlet-mapping> 
            <servlet-name>PrintEnv</servlet-name>
    
            <url-pattern>/servlet/PrintEnv</url-pattern> 
        </servlet-mapping> 
    
  4. Restart Tomcat
  5. Point your browser to http://127.0.0.1:yourPortNumber/csc309/servlet/PrintEnv

Debugging Servlets

Debugging servlets is a little more complex than one would hope. In a nutshell, what you need to do is start Tomcat in debugging mode, and then attach a debugger to the running instance of Tomcat.

This section describes how to debug servlets using the Java Platform Debugger Architecture (JPDA) and the NetBeans IDE.

To debug the HelloWorld servlet described in Section 1.8.2 follow these steps:
  1. Compile the servlet with the -g flag so that the javac compiler generates all debugging information.
  2. Set the environment variable JPDA_ADDRESS to your assigned port number + 1000. For example, if your assigned port number is 30901, you can set the environment variable as follows:

    sh-2.05b$ JPDA_ADDRESS=31901
    sh-2.05b$ export JPDA_ADDRESS

    Tomcat listens on the JPDA_ADDRESS port for debuggers that want to attach the running servlet container.

  3. Start Tomcat in debugging mode using the catalina.sh script. Type:

    sh-2.05b$ catalina.sh jpda start


  4. Start the NerBeans IDE by executing the following script:

    /u/csc309h/lib/NetBeans3.6/bin/runide.sh


  5. Under the Debug menu, click on "Start Session" and then on "Attach..."
  6. In the Attach dialog box, type the name of the "Host" where Tomcat is running (typically localhost), and the "Port" number to which you set the environment variable JPDA_ADDRESS (31901 in out example).
  7. If, everything went well you should see the following messages on the "Debugger Console" tab in the lower left corner of the window:

    Connecting to localhost:31901
    Connection established

    Otherwise, you will get a message box saying something like "Cannot attach. Check the parameters and try again." This is an indication that the debugger was not able to connect to Tomcat. Make sure that the environment variable $JPDA_ADDRESS is set to the right value and that you are starting Tomcat in debugging mode (see Step 3).

  8. Select the "Runtime" tab on the top left corner of the window. Click on "Debugger" and then again on "Classes." You should see a list of all the classes that are currently loaded into Tomcat.
  9. Point your browser to http://127.0.0.1:yourPortNumber/csc309/servlet/HelloWorld
  10. Go back to NetBeans, the class HelloWorld should have been added to the list of available classes.
  11. Double click on HelloWorld.