Jetty Servlet Example - 4ICT12 Assignment 3

Jetty is a 100% Java HTTP Server and Servlet Container which we can use as an alternative to Tomcat

Its sweet because how easy it is to use.

I've put together the simplest example I can conceive to deploy a servlet and launch the server.

Example Servlet

The following servlet simply returns a text document with the classic text Hello World!

package dacurran.example;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloWorldServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
    {
        httpServletResponse.setContentType("text/plain");
        PrintWriter out = httpServletResponse.getWriter();
        out.println("Hello World!");
        out.close();
    }
}
        

This should be familiar to you so I won't dwell on it.

Launching Jetty

The next class, which I call JettyLauncher, simply contains a main method which will configure a server, deploy a servlet and start the server.

package dacurran.example;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHttpContext;

public class JettyLauncher
{
    public static void main(String[] args) throws Exception
    {
        //We will create our server running at http://localhost:8070
        Server server = new Server();
        server.addListener(":8070");

        //We will deploy our servlet to the server at the path '/'
        //it will be available at http://localhost:8070
        ServletHttpContext context = (ServletHttpContext) server.getContext("/");
        context.addServlet("/", "dacurran.example.HelloWorldServlet");

        server.start();
    }
}
        

This will not terminate by itself, so its up to you to kill it, either by pressing stop in your IDE or perhaps Ctrl+C in a cmd prompt in windows. Note that if you try to run this twice the second occurance will fail since you cannot bind two servers to the same port, so take care to stop your server each time before running a new one.

Of course that would all be way too simple if it just worked like that. We need to include some jars on our classpath to make jetty work.

The sample which I include below contains all the sample code and jars required aswell as an ant script to sompile and run the server. I'm not sure if ant is on any of the machines, but you should be able to figure it out. Download from http://ant.apache.org/. Extract and run ant in the root of the sample directory. I.e. in the directory where build.xml file is.

I suggest at least skimming the tutorials on Jetty's Website.

If you are compiling/running from the command line be sure to include ./lib/jetty/org.mortbay.jetty.jar and ./lib/jetty/javax.servlet.jar on the classpath. But again I'd recommend skimming around http://ant.apache.org/

Sample Code to download

Feel free to make use of the following, whatever way you see fit. JettySample.zip