JettyConfiguration Example
We use etc/jetty.xml as an example:

This file configures:
Overview

Jetty3 is configured using XML. All Jetty config files should start like this:
  <?xml version="1.0"  encoding="ISO-8859-1"?>
  <!DOCTYPE Configure SYSTEM "configure.dtd">
  <Configure class="org.mortbay.http.HttpServer">
The top level tag allows configuration of an object of any type. For Jetty this is normally an object of type org.mortbay.http.HttpServer, or some subtype. The configuration interpreter checks that the object is an instance of that class named by the "class" attribute.


Play by Play of jetty.xml

As we go through the configuration, we will show Java-like pseudocode corresponding to the actions requested by the config file. The expressions are real Java code, but the variable names are just to help the reader, and do not mention the variable's type.

First it adds a Listener to the server object with:
  <Call name="addListener">
    <Arg>
      <New class="org.mortbay.http.SocketListener">
        <Set name="Port">8080</Set>
        <Set name="MinThreads">5</Set>
        <Set name="MaxThreads">255</Set>
        <Set name="MaxIdleTimeMs">50000</Set>
      </New>
    </Arg>
  </Call>
A Listener is a network interface object that accepts HTTP requests for the server. SocketListeners accept normal requests, while JsseListeners accept SSL requests. The threading model of the server is controlled by the listener parameters.

This adds a new listener of class org.mortbay.http.SocketListener to the HttpServer as if by:
  listener = server.addListener(new org.mortbay.http.SocketListener());
The tags correspond to:
 listener.setPort(8080);
  listener.setMinThreads(5);
  listener.setMaxThreads(255);
  listener.setMaxIdleTimeMs(50000);

Adding a Web Application

A WebApplication  is a bundled collection of resources, servlets and configuration that can provide a unified WWW application. It is part of the 2.2 servlet standard. The contents of the application are configured by the web.xml deployment descriptor within the application. The configuration of the application within Jetty requires on the context of the application to be set.
  <Call name="addWebApplication">
    <Arg>/default/*</Arg>
    <Arg><SystemProperty name="user.dir"/>/webapps/default/</Arg>
  </Call>
These tags correspond to:
  server.addWebApplication(
    "/default/*",
    System.getProperty("user.dir")+"/webapps/default"
  );

Adding Servlets and Other Handlers

Jetty groups Servlets and other handlers into Contexts. A Context is a grouping of server resources that share the same URL path prefix, class path and resource base. A Web application is an example of a specific context. Generic contexts may have arbitrary request handlers added to them. All contexts have a path specification (frequently the default "*" [[??? -cp]]) and an optional virtual host alias.

Request URL paths matching this pattern will be further matched against the patterns for the Handlers and Servlets in the Context. The request's path will have to match the Context's path, and the remainder of the request path will need to match one of the path specs of the Servlet or Handler.

The following context allows servlet classes placed in the ./servlets directory to be dynamically loaded.
  <Call name="addContext">
    <Arg>/servlet/*</Arg>
    <Set name="ClassPath">./servlets/</Set>
    <Set name="ServingDynamicServlets">TRUE</Set>
  </Call>
As Java code, this is:
  context = server.addContext("/servlet/*");
  context.setClassPath("./servlet/");
  context.setServingDynamicServlet(true);


The second context is similar, but is set to serve resources.
  <Call name="addContext">
    <Arg>/*</Arg>
    <Set name="ResourceBase">./docroot/</Set>
    <Set name="ServingResources">TRUE</Set>
Handlers are the objects that actually service the HTTP requests. Examples of Handlers include ServletHandler, ResourceHandler and NotFoundHandler. Handlers are contained within Contexts, which provide convenience methods for the common handlers so that servlet and file serving may be concisely configured for a context.

To this context we add the "Dump" servlet:
    <Call name="addServlet">
      <Arg>Dump</Arg>
      <Arg>/dump/*:/handler/Dump/*</Arg>
      <Arg>org.mortbay.servlet.Dump</Arg>
    </Call>
In pseudo-Java, this is:
    context.addServlet(
      "Dump",
      "/dump/*:/handler/Dump/*",
      "com.mortaby.Servlet.Dump"
    );
The last adds a non-Servlet Handler to the context:
    <Call name="addHandler">
      <Arg><New class="org.mortbay.http.handler.NotFoundHandler"/></Arg>
    </Call>
As Java:
    context.addHandler(new org.mortbay.http.handler.NotFoundHandler());

Return to JettyConfiguration.