Using the Jetty Webserver for Servlet Programming



Install Jetty


Modify a configuration file to add your own port number


Test your first servlet

Now you are ready to run Jetty and test a servlet.

First, we will run Jetty and try a default servlet:

  • Running Jetty:
    • Go to the $JETTY_HOME directory.
    • Run the Demo Jetty Server using:
              java com.mortbay.Jetty.Demo
            
      Did it run? Did you remember to log in and out to re-set your CLASSPATH?
    • Next fire up a browser.
    • If your assigned port number is 8019, then type in the following URL into the browser: http://localhost:8019.
    • This should deliver an HTML file into your browser from the Jetty Server. Jetty is now working.
    • To stop Jetty: simply kill the server by typing ctrl-C in the window in which you fired up the server.
  • Testing Jetty with your own HTML:
    • First create any old HTML file, say test.html.
    • Put the file in $JETTY_HOME/docroot/test.html.
    • Now, use the URL http://localhost:8019/demo/test.html.
  • Next, Jetty often comes with a test-servlet called HelloWorldServlet. Let's try to access this servlet:
    • First, go to the $JETTY_HOME/servlets directory and see if the HelloWorldServlet.class is actually there.
    • If so, then type the URL http://localhost:8019/servlet/HelloWorldServlet into the browser.
    • This should cause "Hello World" to be displayed in the browser. It is the servlet code that writes "Hello World".

Now you are ready to type, compile and run your own servlet:

  • First, type it in (anywhere):
          import javax.servlet.*;
          import javax.servlet.http.*;
          import java.io.*;
    
          public class HelloWorld extends HttpServlet {
    
            public void doGet (HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException
            {
              // Set the content type of the response.
              resp.setContentType ("text/html");
    
              // Create a PrintWriter to write the response.
              java.io.PrintWriter out = new PrintWriter (resp.getOutputStream());
    
              // The first part of the response.
              out.println ("");
              out.println (" Test ");
              out.println ("");
    
              // The greeting.
              out.println ("Yo, Hello World!");
    
              // Last part.
              out.println ("");
              out.println ("");
              out.close();
    
              // Screen I/O
              System.out.println ("Inside servlet ... servlet complete");
            }
    
            public void doPost (HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException
            {
              doGet (req, resp);
            }
    
          }
      
  • Compile and store the class file in the servlets directory:
          % javac HelloWorld.java
          % cp HelloWorld.class $JETTY_HOME/servlets/.
      
  • Now, fire up Jetty again and enter the URL (Use your port number):
          http://localhost:8019/servlet/HelloWorld
      
  • This should cause the HelloWorld servlet to execute and write the string to the browser.
  • If this did not work, do not proceed until you get this working.
Let's take this one step further:
  • Create the following test.html file:
            <html>
            <head><title> test </title></head>
            <body>
            <a href="http://localhost:8502/servlets/HelloWorld"> Click
              here for a classic greeting </a>
            </body>
            </html>
      
    and save it in the docroot directory.
  • Now access the file in netscape with the URL http://localhost:8502/test.html
  • Click on the above link - the servlet should run.