jLuger.de - A simple standalone HTTP server with jetty

Have you ever wanted create a simple HTTP-Server for playing around, presenting small data or doing some other stuff? Well, jetty offers the possibility to be embedded into your own project. They even have a pretty nice tutorial at http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty. There is sample code and they explain what it is doing but there are no information what jars you need.
This post is about getting the code to run. Using the code from the tutorial page you should end up with something like this:

package de.jluger.server.sample.jetty;


public class HelloHandler extends AbstractHandler {
    public void handle(String target, Request baseRequest,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.setHandler(new HelloHandler());

        server.start();
        server.join();
    }

}

When you've got it in your IDE you will see a lot of red lines complaining about missing imports. Except for the IOException you need libraries from the jetty project to resolve the imports. I've used jetty-distribution-7.1.6.v20100715. Your download will probably have another date.

All the jars you need are under jetty-distribution-7.1.6.v20100715/lib.

First add jetty-server-7.1.6.v20100715.jar to your project. That will provide the following classes:
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

Then add servlet-api-2.5.jar  to the project and you will get this classes:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Now you are rid of most red error markers except for the following three:
HelloHandler The hierarchy of the type HelloHandler is inconsistent
server.start(); The method start() is undefined for the type Server
AbstractHandler The type org.eclipse.jetty.util.component.LifeCycle cannot be resolved. It is indirectly referenced from required .class files
While the first two messages don't give us much help what is wrong the last one requests a concrete class.

This class is found in jetty-util-7.1.6.v20100715.jar. Once you've add it the other two errors will also disappear.

Now try to start the application. If you are using Eclipse use Run As->Java Application. After startup the application will quit immediately and print the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/http/HttpBuffers

The HttpBuffers is in the jar jetty-http-7.1.6.v20100715.jar. Adding the library to the project and starting the application again will get you a similar result like before. Except that the error message is different:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/io/Buffers

Add jetty-io-7.1.6.v20100715.jar and restart the application. No error should appear. Now the application can be tested. Open your favorite browser and enter http://localhost:8080. Nothing should happen but when you switch back to your IDE you should see Exceptions en masse. Here is a sample:
java.lang.NoClassDefFoundError: org/eclipse/jetty/continuation/ContinuationThrowable

For this one add jetty-continuation-7.1.6.v20100715.jar and restart. When you now enter the address in your browser you should see Hello World in a large bold font.

That was a lot of work to deliver a simple Hello world page. So what do you get? First it is very small. You use only 756,1 KB of libraries. The second point is speed. The whole application starts pretty fast. So when you need a simple HTTP server here you go.

Sounds like a Happy End? Be warned. When you need more features of jetty you will have to search for additional libraries. Additionally you will either end up hard wiring a lot of options or you are going to rewrite the configuration management of Jetty. Oh, and don't forget how happy the administration will be when they can't use the standard deployment they know but have to use your own config schema. So if you develop for business customers with their own administration or when you will likely use a lot of Web-App features, don't use this tutorial. Instead read a good tutorial about how to speed up Jetty or Servlet Container in general.