Tims Servlet Tutorial

Tutorial Home

  1. What are servlets ?
  2. The Hello World Servlet
  3. Running Servlets
  4. Reading Form Data
  5. Servlet Configuration
  6. Client Side Form Validation
  7. Cookies
  8. Sessions
  9. Authentication
  10. Dynamic Images
  11. Design, Testing, Security
  12. Servlets and Apache
  13. Using Servlets in SOCS

Running Servlets


Different releases of the Java Servlet Development Kit have included different utilities for testing and running servlets. The way to use these utilities to run the HelloWorldServlet is described below. Please ensure that you set up the relevant servlet packages before attempting this. Instructions for students in SOCS are here.


JSDK 2.0 : The Servlet Runner

The servletrunner is a cut down webserver that can be easily run from the command line and allows the user to test servlets and view any debugging output/error messages that may be generated. The servletrunner is very easy to use, but it is also very limited because it can only serve servlets. Html pages and other content cannot be served by this utility, which makes it quite difficult to use to test more complex applications. In order to run the HelloWorldServlet try typing something like this at the command line :-

servletrunner -d /path/to/servlet/dir

Note : I have found that the servletrunner needs an absolute path specified, it tends to get confused by relative paths.

Once the servletrunner has started, you should be able to access the HelloWorldServlet by using the following address in your web-browser :

http://localhost:8080/servlet/HelloWorldServlet
or
http://machinename:8080/servlet/HelloWorldServlet
(if you are on a networked system)

Unless your feeling particularly masochistic, use the JSDK 2.1 server below.


JSDK 2.1 : The JSDK Server (Reccomended)

This is much improved version of the servletrunner which can serve both servlets and other content, such as html pages. It is however, a little more difficult to use and requires a small amount of set up work to be done before it can be used. If you follow the steps listed below everything should work ! :

  1. Create a directory for your servlet work (eg webwork). HTML files and other content can be placed here.
  2. Create a second directory within this called WEB-INF.
  3. Create a third directory in WEB-INF called servlets. Place your servlet(s) .java and .class files in this directory. Packages directories can also be placed here.
  4. Create a blank text file called servlets.properties and place it in WEB-INF.
  5. Create a file called 'default.cfg' in the webwork directory. Put the following in this file :

    server.port=5000
    server.docbase=/home/loginname/webwork
    server.tempdir=tmp
    server.hostname=
    server.inet=

    Be sure to change the directory path for the docbase property so that it is pointing at the location of your webwork directory. You may also want to change the port number.
  6. Change to the 'webwork' (or whatever) directory and execute the 'startserver' command.
  7. Access you servlets as shown below :
    http://localhost:5000/servlet/HelloWorldServlet
    or
    http://machinename:5000/servlet/HelloWorldServlet
    (if you are on a networked system)

    Web content (eg html files) are mapped to / on the server, so a file called index.html placed in the webwork directory will found be on http://localhost:5000/index.html (or equivalent)
  8. When you have finished testing your servlet, run the 'stopserver' command. This is especially important if you work in a networked environment or share a workstation.

Proxy Servers

If you have your browser setup to use a proxy, you need to ensure that the proxy is bypassed to use 'localhost'. If accessing using a local machine name, you should bypass this address too, or you may have problems with the proxy caching old versions of the page.


<< Previous Page | Next Page >>




Tims Home Page       |       Page last modified : 19 May 2003