Contents

Introduction

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.

Details

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

Create the server

Add/Configure Connectors

Add/Configure Handlers

Add/Configure Servlets/Webapps to Handlers

start the server

wait (join the server to prevent main exiting).

Servers

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

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.

Connectors

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 ManyConnectors.java and shows how connectors may be set and configured for the Hello world example:

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.

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).

Contexts

A ContextHandler is a HandlerWrapper that will respond only to requests that have a URI prefix that match the configured context path.

Requests that match the context path will have their path methods updated accordingly and the following optional context features applied as appropriate:

* A Thread Context classloader.
* A set of attributes
* A set init parameters
* A resource base (aka document root)
* A set of virtual host names

Requests that don't match are not handled.

The following code is based on OneContext.java and sets context path and classloader for the hello handler:

Servlets

Servlets are the standard way to provide application logic that handles HTTP requests. Servlets are like constrained Handlers with standard ways to map specific URIs to specific servlets. The following code is based on HelloServlet.java: