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:
- Java Servlet Tutorial
- Jakarta-Tomcat (De facto standard servlet engine/container) - http://jakarta.apache.org/tomcat/index.html
- Jo! (another servlet engine) - http://www.tagtraum.com/jo.html
- Jetty (another servlet engine) - http://jetty.mortbay.com/
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:
- 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
- Include the following jar file in your CLASSPATH /u/csc309h/lib/tomcat-5.0.28/common/lib/servlet-api.jar.
- Start Tomcat
- 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:- Copy PrintEnv.java to $CATALINA_HOME/webapps/csc309/WEB-INF/classes
- Compile PrintEnv.java
- 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> - Restart Tomcat
- 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:
- Compile the servlet with the -g flag so that the javac compiler generates all debugging information.
- 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.
- Start Tomcat in debugging mode using the catalina.sh script. Type:
sh-2.05b$ catalina.sh jpda start
- Start the NerBeans IDE by executing the following script:
/u/csc309h/lib/NetBeans3.6/bin/runide.sh
- Under the Debug menu, click on "Start Session" and then on "Attach..."
- 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).
- 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).
- 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.
- Point your browser to http://127.0.0.1:yourPortNumber/csc309/servlet/HelloWorld
- Go back to NetBeans, the class HelloWorld should have been added to the list of available classes.
- Double click on HelloWorld.
