Creating and Deploying Servlets

We will be using the TomCat webserver extension for Servlets. Because most universities do not allow servlets (at this writing), you will need to download a webserver extension application to your pc, compile servlets there and then launch them. The following tells you how!

  1. Installing Tomcat

    1. Download and install jakarta-tomcat-4.0.1.exe from http://jakarta.apache.org/builds/jakarta-tomcat-4.0/archives/v4.0.1/bin/

  2. Starting Tomcat
    1. Go to the directory C:\Windows\Start Menu \Apache Tomcat4.0 and click on the Start Tomcat icon
      (Or you can click on Start -> Programs ->Apache Tomcat 4.0 -> Start Tomcat)

    2. Now, with Tomcat running, type the following URL into your browser and view documentation and examples: http://localhost:8080 (If "localhost" doesn't work, try "127.0.0.1")

    3. You can stop Tomcat by clicking on the Stop Tomcat icon in C:\Windows\Start Menu \Apache Tomcat4.0 (Or you can click on Start -> Programs ->Apache Tomcat 4.0 -> Stop Tomcat)

  3. Compiling servlets. Servlets are essentially java programs with two new includes:
      import javax.servlet.*;
      import javax.servlet.http.*;
    1. Save your .java file somewhere

    2. Open a DOS window

    3. To compile either

      • copy the the servlet package to your JDK library
        C:\Program Files\Apache Tomcat 4.0\common\lib -->
        C:\jdk1.3.1+02\jre\lib\ext and (yes, both!) C:\jdk1.3.1_02\lib

        and type
        javac somewhere/your java servlet file

        OR

      • specify the location of the servlet jar file. Type:
        javac -classpath "c:\Program Files\Apache Tomcat 4.0\common\lib\servlet.jar somewhere/your java servlet file

  4. Running Servlets
    1. Copy your servlet .class file to the directory:

      C:\Program Files\Apache Tomcat 4.0\webapps\examples\WEB-INF\classes\

    2. Restart your Tomcat server (First "Stop TomCat" - see above). Everytime you put a new servlet in the directory, you must restart the Tomcat server

    3. Either:

      • Create an HTML file which deploys the servlet and type its URL into a browser window

        OR type the following URL directly into a browser window:

        http:// localhost:8080/examples/servlet/ Your Servlet File / (Don't type .class)