Table of Contents
To understand Jetty Configuration, you need to understand the "How" and the "What". This section covers how to configure Jetty in terms of what mechanisms exist to perform configuration. The next section will give an overview of the action components and fields that can be configured with these mechanisms.
The main components of Jetty are simply Plain Old Java Objects ( POJOs) and the process of configuring jetty can be considered mostly as the process of instantiating, assembling and setting fields on the Jetty POJOs. This can be achieved either by:
Writing java code to directly instantiate and assemble Jetty objects. This is referred to as ??? Jetty.
Using Jetty XML Configuration, which is a Inversion of Control (IoC) framework, to instantiate and assemble Jetty object as XML objects
Using a third party IoC framework like Spring, to instantiate and assemble Jetty objects as spring beans.
Because the main Jetty configuration is done by IoC, it is the Jetty API documentation that is the ultimate configuration reference.
Some Jetty Components do have configuration files that are not IoC
The Servlet Specification defines the web.xml deployment descriptor that defines and configures the Filters, Servlets and resources used by a Web Application. The Jetty WebAppContext component uses this XML format to:
set up the default configuration of a web application context
interpret the application specific configuration supplied with a web application in the WEB-INF/web.xml file
interpret descriptor fragments included in the META-INF directory of jar files within WEB-INF/lib
Standard java property files are also used for jetty configuration in several ways:
to parameterize Jetty IoC XML via the use of the Property element
to configure the default logging mechanism (StdErrLog). Other logging frameworks may also be plugged into jetty and also use property files (eg log4j)
as a simple database for login usernames and credentials
The Jetty start mechanism uses a ini file to hold command line arguments that would otherwise have to be passed to the command to start Jetty.
An example of a Jetty server assembled and configured in embedded style is:
package org.eclipse.jetty.embedded; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.servlet.ServletContextHandler; public class ExampleServer { public static void main(String[] args) throws Exception { Server server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(8080); server.setConnectors(new Connector[] { connector }); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/hello"); context.addServlet(HelloServlet.class, "/"); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[] { context, new DefaultHandler() }); server.setHandler(handlers); server.start(); server.join(); } }
The exact same server configuration can be achieve with the Jetty XML file:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure id="ExampleServer" class="org.eclipse.jetty.server.Server"> <Set name="connectors"> <Array type="org.eclipse.jetty.server.Connector"> <Item> <New class="org.eclipse.jetty.server.ServerConnector"> <Arg><Ref id="ExampleServer"/></Arg> <Set name="port">8080</Set> </New> </Item> </Array> </Set> <New id="context" class="org.eclipse.jetty.servlet.ServletContextHandler"> <Set name="contextPath">/hello</Set> <Call name="addServlet"> <Arg>org.eclipse.jetty.embedded.HelloServlet</Arg> <Arg>/</Arg> </Call> </New> <Set name="handler"> <New class="org.eclipse.jetty.server.handler.HandlerCollection"> <Set name="handlers"> <Array type="org.eclipse.jetty.server.Handler"> <Item> <Ref id="context" /> </Item> <Item> <New class="org.eclipse.jetty.server.handler.DefaultHandler" /> </Item> </Array> </Set> </New> </Set> </Configure>
In practise, most commonly used Jetty features have had xml files created for them that are included in the standard distribution in the etc directory. Thus configuring Jetty is often a matter of just editing the existing XML files and altering the configuration values within them.
With a normal distribution of Jetty, the configuration mechanism introduced above are typically used as follows:
Used to enable/disable the jetty features by setting OPTIONS that setup the classpath and declaring which configuration files are included on the command line.
Jetty IoC XML files that configure individual features; eg jetty.xml (for the server), jetty-http.xml, jetty-https.xml, jetty-jmx.xml etc
The directory in which standard WAR files, web applications and context IoC XML files are deployed