Skip to main content
Jump to: navigation, search

Jetty/Tutorial/Embedding Jetty


Jetty has a slogan "Don't deploy your application in Jetty, deploy Jetty in your application". What this means is that Jetty as an alternative to bundling your application as a standard WAR to be deployed in Jetty, Jetty is designed to be a software component that can be instantiated and used in a java program just like any POJO.

This tutorial takes you step by step from the simplest jetty server instantiation, through programmatically, to running multiple web applications with standards based deployment descriptors.

The source for most of these examples is part of the standard jetty project.


To embed a Jetty server, the following steps are typical:

  1. Create the server
  2. Add/Configure Connectors
  3. Add/Configure Handlers
  4. Add/Configure Servlets/Webapps to Handlers
  5. start the server
  6. wait (join the server to prevent main exiting).


The following code from will instantiate and run the simplest possible Jetty server:

public class SimplestServer
    public static void main(String[] args) throws Exception
        Server server = new Server(8080);

This runs a HTTP server on port 8080. It is not a very useful server as it has not handlers and thus will return a 404 error for every request.


In order to produce a response to a request, Jetty requires a Handler to be set on the server. A handler may:

Hello World Handler

The following code based on shows a simple hello world handler:

public class HelloHandler extends AbstractHandler
    public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
        throws IOException, ServletException
        response.getWriter().println("<h1>Hello World</h1>");

The parameters passed to the handle method are:

  • target - The target of the request which is either a URI or a name from a named dispatcher.
  • baseRequest -The Jetty mutable request object, which is always unwrapped.
  • request - The immutable request object, which may have been wrapped.
  • response - The response that may have been wrapped.

The handler sets the response status, content-type and marks the request as handled before it generates the body of the response using a writer.

The following code from shows how this handler can be used by a Jetty server:

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

You now know everything you need to know to write a HTTP server based on Jetty. However, complex request handling is typically built from multiple Handlers and we will look in later sections how handlers can be combined like aspects. Some of the handlers available in Jetty can be seen in the org.eclipse.jetty.server.handler package.


In order to configure the HTTP connectors used by the server, one or more Connectors may be set on the server. Each connector may be configured with details such as interface, port, buffer sizes, timeouts etc.

The following code is based on and shows how connectors may be set and configured for the Hello world example:

public class ManyConnectors
    public static void main(String[] args) throws Exception
        Server server = new Server();

        SelectChannelConnector connector0 = new SelectChannelConnector();

        SelectChannelConnector connector1 = new SelectChannelConnector();
        connector1.setThreadPool(new QueuedThreadPool(20));

        SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
        String jetty_home = System.getProperty("jetty.home","../jetty-distribution/target/distribution");
        ssl_connector.setKeystore(jetty_home + "/etc/keystore");

        server.setConnectors(new Connector[]{ connector0, connector1, ssl_connector });

        server.setHandler(new HelloHandler());


Handler Collections, wrappers and Scopes

Complex request handling is typically built from multiple Handlers that can be combined in various ways:

  • A Handler Collection holds a collection of other handlers and will call each handler in order. This is useful for combining statistics and logging handlers with the handler that generates the response.
  • A Handler List is a Handler Collection that calls each handler in turn until either an exception is thrown, the response is committed or the request.isHandled() returns true. It can be used to combine handlers that conditionally handle a request.
  • A Handler Wrapper is a handler base class that can be use to daisy chain handlers together in the style of aspect oriented programming. For example, a standard web application is implemented by a chain of a context, session, security and servlet handlers.

File Server

The following code from uses a HandlerList to combine the ResourceHandler with the DefaultHandler:

public class FileServer
    public static void main(String[] args) throws Exception
        Server server = new Server(8080);

        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setWelcomeFiles(new String[]{ "index.html" });

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });


The resource handler is passed the request first and looks for a matching file in the local directory to serve. If a file is not found, then the request is passed to the default handler which will generate a 404 (or favicon.ico).





Back to the top