This section describes how to use Jetty for a number of basic use cases.
Serving Static Content
scripting to change bar colors on a chart based on plotted data. When running Jetty as an embedded web server in your own application, it can be useful to be able to serve static content (e.g., documentation). The easiest way to server static content is by using a ResourceHandler:
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="directoryListed">true</Set>
<Set name="resourceBase">./files</Set>
</New>
The ResourceHandler can be configured with the following properties:
Property |
Description |
aliases |
Boolean; if true symlinks are followed. |
directoryListed |
Boolean; if true show directory listings. |
welcomeFiles |
String[]; a welcome file is shown for a directory if it matches an item from the supplied String[]. |
resourceBase |
String: The path from where to serve content. |
If you want the ResourceHandler to listen on a specific context, you can wrap this Handler in a ContextHandler:
<New class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/documentation</Set>
<Set name="handler">
<New class="...ResourceHandler">...</New>
</Set>
</New>
SSL Configuration
To configure Jetty to use HTTPS, you can use one of the SSL-enabled connectors: SslSelectChannelConnector or SSLSocketConnector. The following listing defines two-way ssl (client authentication):
Call name="addConnector">
<Arg>
<New class="org.eclipse...SslSelectChannelConnector">
<Arg>
<New class="org.eclipse.jetty.http.ssl.SslContextFactory">
<Set name="keyStore">etc/keystore</Set>
<Set name="keyStorePassword">OBF:1vny1zlo1x8e1vnw1</Set>
<Set name="keyManagerPassword">OBF:1u2u1wml1z7s1z/Set>
<Set name="trustStore">/etc/keystore</Set>
<Set name="trustStorePassword">OBF:w11x8g1zlu1vn4</Set>
<Set name="needClientAuth">true</Set>
</New>
</Arg>
<Set name="port">8443</Set>
</New>
</Arg>
</Call>
The following properties are used:
Property |
Description |
keystore |
Keystore for the server keypair |
keystorepassword |
Password for the keystore |
keymanagerpassword |
Password for the private key |
truststore |
Keystore for trusted certificates |
truststorepassword |
Password for truststore |
needClientAuth |
True, if clients must use a certificate |
There are more advanced properties available. For these, see the Javadocs for the SslContextFactory.
Jetty provides a utility that you can use to secure passwords in configuration files. By using the org.eclipse.jetty.http.security.Passwd class, you can generate obfuscated, checksummed, and encrypted passwords.
Servlets and the ServletContextHandler
Jetty allows you to easily configure servlets without having to use a web. xml. To do this, you can use a ServletContextHandler, which allows for easy construction of a context with a ServletHandler. The following properties can be set on a ServletContextHandler.
Property |
Description |
contextPath |
The base context path used for this ServletContextHandler. |
allowNullPathInfo |
If "false", then /context is redirected to /context/. |
compactPath |
If "true", replace multiple '/'s with a single '/'. |
errorHandler |
An "ErrorHandler" determines how error pages are handled. |
securityHandler |
The "SecurityHandler" to use for this ServletContextHandler. |
sessionHandler |
The "SessionHandler" to use for this ServletContextHandler |
welcomeFiles |
List of welcomeFiles to show for this context. |
A servlet on this context can be added using the addServlet operation (which can also be done through XML).
addServlet(String className,String pathSpec)
addServlet(Class<? extends Servlet> servlet,String pathSpec)
addServlet(ServletHolder servlet,String pathSpec)
Using Existing WAR Files and Layout
Jetty allows you to directly use existing WAR files (and exploded WAR files) through the WebAppContext. This is especially useful during development.
Directly from a WAR:
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(jetty_home+"/webapps/test.war");
server.setHandler(webapp);
From an Exploded WAR (e.g During Development):
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setDescriptor(webapp+"/WEB-INF/web.xml");
context.setResourceBase("../test-jetty-webapp/src/main/webapp");
context.setContextPath("/");
context.setParentLoaderPriority(true);
server.setHandler(context);
You can also configure a WebAppContext in jetty.xml using the XMLbased configuration or in a context.xml file.
Security Realms
Security Realms allow you to protect your web applications. Jetty provides a number of standard LoginServices you can use to secure your web application.
Property |
Description |
HashLoginService |
A simple implementation that stores users and roles in memory and loads them from a properties file. |
JDBCLoginService |
Retrieves users and roles from a database configured with JDBC. |
DataSourceLogin- Service |
Retrieves users and roles from a javax.sql.DataSource. |
JAASLoginService |
Delegates the login to JAAS. Jetty provides the following JAAS modules: • DBCLoginModule • PropertyFileLoginModule • DataSourceLoginModule • LdapLoginModule |
To use a LoginServices, you configure it in the jetty.xml file.
<Call name="addBean">
<Arg>
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">RefCardRealm</Set>
<Set name="config">etc/realm.properties</Set>
<Set name="refreshInterval">0</Set>
</New>
</Arg>
</Call>
The real-name defined in the jetty.xml can now be referenced from the web.xml.
<login-config>
<auth-method>FORM</auth-method>
<realm-name>RefCardRealm</realm-name>
<form-login-config>
<form-login-page>/login/login</form-login-page>
<form-error-page>/login/error</form-error-page>
</form-login-config>
</login-config>
JNDI Usage
Jetty has support for java:comp/env lookups in web applications.
Setup JNDI
The JNDI feature isn't enabled by default. To enable JNDI, you have to set the following configurationClasses on your WebAppContext or WebAppDeployer:
Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<Array id="plusConfig" type="java.lang.String">
<Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
<Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
<Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
<Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
<Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
<!-- next one not needed for Jetty 8 -->
<Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
</Array>
<Set name="war">location/of/webapp</Set>
<Set name="configurationClasses"><Ref id="plusConfig"/></Set>
</Configure>
You can now use , , and entries in your web.xml to point to resources stored in the JNDI context.
Binding Objects to JNDI
Jetty allows you to bind POJOs, a java.naming.Reference instance, an implementation of java.naming.Referenceable, and a link between a name in the web.xml and one of these other objects. These objects can be configured from Java or in the jetty.xml configuration files.
<New class=type of naming entry>
<Arg>scope</Arg>
<Arg>name to bind as</Arg>
<Arg>the object to bind</Arg>
</New>
The scope defines where the object is visible. If left empty, the scope is set to the Configure context this entry is defined in. This scope can also be set to point to a specific server of webapplication.
<Arg><Ref id='wac'/></Arg>
The following environment types are supported:
Type |
How to bind from Jetty |
env-entry |
<New class="org.eclipse.jetty.plus.jndi.EnvEntry"> <Arg></Arg> <Arg>mySpecialValue</Arg> <Arg type="java.lang.Integer">4000</Arg> <!—set to true to override web.xml --> <Arg type="boolean">true</Arg> </New> |
resource-ref resourceenv- ref |
<New id="myds" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg><Ref id="wac"/></Arg> <Arg>jdbc/myds</Arg> <Arg> <New class="org...EmbeddedDataSource"> <Set name="DatabaseName">test</Set> <Set name="createDatabase">create</Set> </New> </Arg> </New> |
Link |
With a Link you can link a resource from the web.xml to a resource in the containter context. <New id="map1" class="org.eclipse.jetty.plus.jndi.Link"> <Arg><Ref id='wac'/></Arg> <Arg>jdbc/datasourceInWeb</Arg> <Arg>jdbc/nameInContainer</Arg> </New> |
Jetty-env.xml
You can store environment settings in a jetty.xml file that is stored in your WEB-INF directory.
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<!-- Add entries only valid for this webapp -->
</Configure>
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}