Beruflich Dokumente
Kultur Dokumente
Jetty/Tutorial/Embedding Jetty
From Eclipsepedia
Contents
1 Introduction
2 Details
3 Creating a Server
4 Writing Handlers
4.1 Hello World Handler
5 Configuring Connectors
6 Understanding Handler Collections, Wrappers and Scopes
7 Configuring a File Server
7.1 Configuring a File Server with XML
7.2 Using Spring to Configure a File Server
8 Setting Contexts
9 Creating Servlets
10 Setting a ServletContext
11 Setting a Web Application Context
12 Configuring a Context Handler Collection
13 Embedding JSP
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
(http://en.wikipedia.org/wiki/Plain_Old_Java_Object) .
This tutorial takes you step-by-step from the simplest Jetty server instantiation to running multiple web applications
with standards-based deployment descriptors.
Details
To embed a Jetty server, the following steps are typical:
wiki.eclipse.org/…/Embedding_Jetty 1/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
4. Add/Configure Servlets/Webapps to Handlers
5. Start the server
6. Wait (join the server to prevent main exiting)
Creating a Server
The following code from SimplestServer.java (http://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk/example-
jetty-embedded/src/main/java/org/eclipse/jetty/embedded/SimplestServer.java) instantiates and runs the simplest
possible Jetty server:
This runs an HTTP server on port 8080. It is not a very useful server as it has no handlers and thus returns a 404
error for every request.
Writing Handlers
To produce a response to a request, Jetty requires a Handler (http://download.eclipse.org/jetty/stable-
7/xref/org/eclipse/jetty/server/Handler.html) to be set on the server. A handler may:
wiki.eclipse.org/…/Embedding_Jetty 2/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
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, which 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.
server.start();
server.join();
}
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
(http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/server/handler/package-frame.html) package.
Configuring Connectors
To configure the HTTP connectors that the server uses, you can set one or more connector
(http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/server/Connector.html) s on the server. You can
configure each connector with details such as interface, port, buffer sizes, timeouts, etc.
wiki.eclipse.org/…/Embedding_Jetty 3/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
server.setHandler(new HelloHandler());
server.start();
server.join();
}
}
wiki.eclipse.org/…/Embedding_Jetty 4/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
See also How to Write a Jetty Handler.
resource_handler.setResourceBase(".");
server.start();
server.join();
}
}
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).
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:
wiki.eclipse.org/…/Embedding_Jetty 5/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/c
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<Set name="port">8080</Set>
</New>
</Arg>
</Call>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerList">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="directoriesListed">true</Set>
<Set name="welcomeFiles">
<Array type="String"><Item>index.html</Item></Array>
</Set>
<Set name="resourceBase">.</Set>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.server.handler.DefaultHandler">
</New>
</Item>
</Array>
</Set>
</New>
</Set>
</Configure>
You can also use the Spring framework to assemble Jetty servers. The file server example above can be written in
wiki.eclipse.org/…/Embedding_Jetty 6/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
Spring configuration as:
<beans>
<bean id="Server" class="org.eclipse.jetty.server.Server" init-method="start" dest
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnec
<property name="port" value="8080"/>
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerList">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.server.handler.ResourceHandler">
<property name="directoriesListed" value="true"/>
<property name="welcomeFiles">
<list>
<value>index.html</value>
</list>
</property>
<property name="resourceBase" value="."/>
</bean>
<bean class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</list>
</property>
</bean>
</property>
</bean>
</beans>
Setting Contexts
A ContextHandler (http://download.eclipse.org/jetty/stable-
7/xref/org/eclipse/jetty/server/handler/ContextHandler.html) is a HandlerWrapper that responds only to requests
that have a URI prefix that matches the configured context path.
Requests that match the context path have their path methods updated accordingly, and the following optional
context features applied as appropriate:
wiki.eclipse.org/…/Embedding_Jetty 7/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
context.setHandler(new HelloHandler());
server.start();
server.join();
}
}
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 (http://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk/example-jetty-
embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java) :
Setting a ServletContext
A ServletContextHandler (http://download.eclipse.org/jetty/stable-
7/xref/org/eclipse/jetty/servlet/ServletContextHandler.html) is a specialization of ContextHandler with support for
standard servlets. The following code from OneServletContext (http://download.eclipse.org/jetty/stable-
7/xref/org/eclipse/jetty/embedded/OneServletContext.html) shows 3 instances of the helloworld servlet registered
with a ServletContextHandler:
wiki.eclipse.org/…/Embedding_Jetty 8/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
server.start();
server.join();
}
}
server.start();
server.join();
}
}
If during development, you have not assembled your application into a war file, you can run it from its source
components with something like:
wiki.eclipse.org/…/Embedding_Jetty 9/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
server.setHandler(context);
server.start();
server.join();
}
}
server.setHandler(contexts);
server.start();
server.join();
}
}
Embedding JSP
wiki.eclipse.org/…/Embedding_Jetty 10/11
24/03/2011 Jetty/Tutorial/Embedding Jetty - Eclip…
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.
[INFO] org.eclipse.jetty:jetty-jsp-2.1:jar:7.2.2-SNAPSHOT
[INFO] +- org.eclipse.jetty:jetty-util:jar:7.2.2-SNAPSHOT:provided
[INFO] +- org.mortbay.jetty:jsp-2.1-glassfish:jar:2.1.v20100127:provided
[INFO] | +- org.eclipse.jdt.core.compiler:ecj:jar:3.5.1:provided
[INFO] | +- org.mortbay.jetty:jsp-api-2.1-glassfish:jar:2.1.v20100127:provided
[INFO] | \- ant:ant:jar:1.6.5:provided
[INFO] \- javax.servlet:servlet-api:jar:2.5:provided
You should be able to depend on one of the embedding aggregates that we provide to get these dependencies
without too much trouble.
http://repo2.maven.org/maven2/org/eclipse/jetty/aggregate
Home
Privacy Policy
Terms of Use
Copyright Agent
Contact
About Eclipsepedia
This page was last modified 20:34, 18 February 2011 by Shirley Boulay. Based on work by Greg Wilkins,
Jesse McConnell and Michael Buck and others.
wiki.eclipse.org/…/Embedding_Jetty 11/11