Embedding jsp support can be a bit confusing if you look at the jars under the lib/jsp directory in the jetty distribution. This is because we have to ship from eclipse with the jsp bundles that are marked up as osgi bundles and these are not directly downloadable from maven central. There are dependencies available in maven central that will work though as they are the ones that were the actual source for the osgi bundles themselves. The osgi bundles are simply these maven central artifacts that have been decomposed into a few extra bundles.

Introduction

Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your application." What this means is that 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 to running multiple web applications with standards-based deployment descriptors.

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

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

Configuring Connectors

To configure the HTTP connectors that the server uses, you can set one or more connectors on the server. You can configure each connector with details such as interface, port, buffer sizes, timeouts, etc.

The following code is based on ManyConnectors.java and shows how to set and configure connectors for the Hello World example:

Understanding 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 calls 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 used 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 generates a 404 (or favicon.ico).

Configuring a File Server with XML

Now is a good time to remind you that the Jetty XML configuration format is able to render simple Java code into XML configuration. So the File Server example above can be written with a little reordering in Jetty XML as follows:

Creating 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:

Setting a ServletContext

A ServletContextHandler is a specialization of ContextHandler with support for standard servlets. The following code from OneServletContext shows 3 instances of the helloworld servlet registered with a ServletContextHandler:

Configuring a Context Handler Collection

A Context Handler Collection uses the longest prefix of the request URI (the contextPath) to select a specific context. The following example combines the previous two examples in a single Jetty server:

Embedding JSP

Embedding jsp support can be a bit confusing if you look at the jars under the lib/jsp directory in the jetty distribution. This is because we have to ship from eclipse with the jsp bundles that are marked up as osgi bundles and these are not directly downloadable from maven central. There are dependencies available in maven central that will work though as they are the ones that were the actual source for the osgi bundles themselves. The osgi bundles are simply these maven central artifacts that have been decomposed into a few extra bundles.