Jetty Tutorial

Jetty is a lightweight Servlet container which we suggest you use if you are going to develop servlets on your local machine, please don't run it on eniac.

Getting Jetty

  1. Make sure you have a Java SDK installed, you can download one here.
  2. Download the latest version of Jetty (6.1.11).
  3. Extract the zip archive to somewhere on your computer.
  4. Open a command line terminal and navigate to the directory where you extracted the archive.
  5. Run the command:
    java -jar start.jar
    The server will start and log messages should start appearing in the terminal.
  6. Open a web browser and enter "http://localhost:8080" in the address bar.
  7. If you see a "Welcome to Jetty 6" page then the server is successfully set up. Return to the terminal and press Control-C to stop the server.

Writing a Simple Web Application

The Jetty server has a directory called "webapps" which contains a directory for every website (web application) it hosts. We will create a simple application and place it in this directory.
  1. Make a new directory called "hello" in "webapps". Files in this directory will be accessible as "http://localhost:8080/hello".
  2. Make a new directory called "WEB-INF" in "hello". This directory will contain configuration information for your application.
  3. Make a new directory called "classes" in "WEB-INF". This directory will contain the .class files for all your servlets.
  4. Create a file "index.html" in "hello". This is the page you will get when you go to "http://localhost:8080/hello". The file should contain the following:
    <head><title>Example Web Application</title></head>
    <p>This is a static document with a form in it.</p>
    <form method="POST" action="servlet"/>
    <input name="field" type="text" />
    <input type="submit" value="Submit" />
    If you start the server at this point and click here you will see this new page.
  5. Next we will create the handler for this form, which will be a Java Servlet. Create a file called with the following code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class HelloServlet extends HttpServlet
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
            String q = req.getParameter("q");
            PrintWriter out = resp.getWriter();
            out.println("The paramter q was \"" + q + "\".");
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
            String field = req.getParameter("field");
            PrintWriter out = resp.getWriter();
            out.println("You entered \"" + field + "\" into the text box.");
  6. To compile this class we need the servlet libraries, which are not part of the Java SDK, but can be found in Jetty. Use the following command (in the directory with to compile your servlet:
    javac -cp PATH_TO_JETTY/lib/servlet-api-2.5-6.1.11.jar
    The "-cp" option adds the given jar file to the list of places to search for other classes your code requires. Copy the resulting HelloServlet.class file into the "hello/WEB-INF/classes" directory.
  7. Now, we must tell Jetty that this class should be used to handle requests. The following "web.xml" file, which goes in the WEB-INF directory, describes these mappings:
    <?xml version="1.0" encoding="UTF-8"?>
      <!-- Declare the existence of a servlet. -->
      <!-- Map URLs to that servlet. -->
  8. If you restart the server you should be able to click here, enter something in the form, and the servlet will respond when you click "Submit".

Note, there are two methods in a servlet class, doGet() and doPost(). These correspond to the two types of HTTP requests. The form we have uses the POST method. It could also use the GET method. The difference is that in the GET method the parameters are passed as part of the URL, which is inappropriate for most web forms. Try entering "http://localhost:8080/hello/servlet?q=Hello" into your browser to see how passing parameters with the GET method works.

If you have any questions, please post to the class newsgroup so others can benefit from the answer.