Jetty has moved!
Jetty is a project at the Eclipse Foundation.
Jetty Powered:
Contact the core Jetty developers at
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 50 Next »


These instructions apply to releases of jetty-6.1.12rc3 and above, and of jetty-7.0.0.pre4 and above. For prior releases, these instructions remain applicable except for the "scoping" parameter of naming entries, which is not present, leaving the format as:

Using JNDI Resources with Jetty

Jetty supports java:comp/env lookups in webapps. This is an optional feature, and as such some setup needs to be done. We'll show you how to do it, although we have provided jetty configuration files that already have JNDI enabled to help you get going faster. More on that later.

Firstly, to enable JNDI for a web application, you need to configure the WebAppContext to parse the web.xml file and perform the java:comp/env linkages. The class that does this is, and we specify its name in the list of configurations to be applied to the webapp when we define the org.mortbay.jetty.webapp.WebAppContext for it. The following example enables naming services for the xyzWebAppContext:

Or, more conveniently, you can specify that these configurations are applied to all webapps deployed by the WebAppDeployer :



To save you some time, we have included the etc/jetty-plus.xml configuration file that configures a WebAppDeployer to deploy all webapps in the webapps-plus directory with JNDI. You can modify this as desired, or merge it with your etc/jetty.xml file to lessen the number of config files on the command line. See here for examples of putting multiple xml files on the jetty run line.

You may now configure naming resources that can be referenced in a web.xml file and accessed from within the java:comp/env naming environment of the webapp during execution. Specifically, you may configure support for the following web.xml elements:

Configuring env-entrys shows you how to set up overrides for <env-entry> elements in web.xml. Configuring resource-refs and resource-env-refs discusses how to configure support resources such as javax.sql.DataSource.

Furthermore, it is possible to plug a JTA javax.transaction.UserTransaction implementation into Jetty so that webapps can lookup java:comp/UserTransaction to obtain a distributed transaction manager. See Configuring XA Transactions.

You can define your naming resources with 3 scopes:

  1. jvm scope - the name is unique within the jvm
  2. server scope - the name is unique to the Server instance
  3. webapp scope - the name is unique to the WebAppContext instance

The section Global or scoped to a webapp explains what scoping is, and shows you how to use it. Essentially, scoping ensures that JNDI bindings from one webapp do not interfere with the JNDI bindings of another - unless of course you wish them to.

Before we go any further, lets take a look at what kind of things can be bound into JNDI with Jetty.

What can be bound and general overview

There are 3 types of objects that can be bound into Jetty's JNDI:

The binding for all of these object types generally follows the same pattern:

<New class=type of naming entry>
<Arg>name to bind as</Arg>
<Arg>the object to bind</Arg>

The type of naming entry can be:

  • "" for <env-entry>s
  • "" for all other type of resources
  • "" for a JTA manager. We'll take a closer look at this in the Configuring XA Transactions section).

There are 3 places in which you can define naming entries:

  1. jetty.xml
  2. WEB-INF/jetty-env.xml
  3. context xml file

Naming entries defined in a jetty.xml file will generally be scoped at either the jvm level or the Server level. Naming entries in a jetty-env.xml file will generally be scoped to the webapp in which the file resides, although you are able to enter jvm or Server scopes if you wish, that is not really recommended. In most cases you will define all naming entries that you want visible to a particular Server instance, or to the jvm as a whole in a jetty.xml file. Entries in a context xml file will generally be scoped at the level of the webapp to which it applies, although once again, you can supply a less strict scoping level of Server or jvm if you want.

Configuring env-entrys

Sometimes it is useful to be able to pass configuration information to a webapp at runtime that either cannot be or is not convenient to be coded into a web.xml <env-entry>. In this case, you can use and even configure them to override an entry of the same name in web.xml.

This example will define a virtual <env-entry> called mySpecialValue with value 4000 that is unique within the whole jvm. It will be put into JNDI at java:comp/env/mySpecialValue. Moreover, the boolean argument indicates that this value should override an env-entry of the same name in web.xml. If you don't want to override, then omit this argument or set it to false.

See Global or scoped to a webapp for more information on other scopes.

Note that the Servlet Specification only allows the following types of object to be bound to an env-entry:

  • java.lang.String
  • java.lang.Integer
  • java.lang.Float
  • java.lang.Double
  • java.lang.Long
  • java.lang.Short
  • java.lang.Character
  • java.lang.Byte
  • java.lang.Boolean

However, Jetty is a little more flexible and will also allow you to bind custom POJOs, javax.naming.References and javax.naming.Referenceables. Be aware if you take advantage of this feature that your web application will not be portable.

To use the EnvEntry configured above, use code in your servlet/filter/etc such as:

Configuring resource-refs and resource-env-refs

Any type of resource that you want to refer to in a web.xml file as a <resource-ref> or <resource-env-ref> can be configured using the type of naming entry. You provide the scope, the name of the object (relative to java:comp/env) and a POJO instance or a javax.naming.Reference instance or javax.naming.Referenceable instance.

The J2EE Specification recommends that DataSources are stored in java:comp/env/jdbc, JMS connection factories under java:comp/env/jms, JavaMail connection factories under java:comp/env/mail and URL connection factories under java:comp/env/url. For example:

Resource Type

Name in jetty.xml

Environment Lookup










Configuring DataSources

Lets look at an example of configuring a javax.sql.DataSource. Jetty can use any DataSource implementation available on it's classpath. In our example, we'll use a DataSource from the Derby relational database, but you can use any implementation of a javax.sql.DataSource. In this example, we'll configure it as scoped to a webapp with the id of 'wac':

The above would create an instance of org.apache.derby.jdbc.EmbeddedDataSource, call the two setter methods setDatabaseName("test"); and setCreateDatabase("create"); and bind it into the JNDI scope for the webapp. If you have the appropriate <resource-ref> setup in your web.xml, then it will be available from application lookups as java:comp/env/jdbc/myds.

To lookup your DataSource in your servlet/filter/etc do:



When configuring Resources, you need to ensure that the type of object you configure matches the type of object you expect to lookup in java:comp/env. For database connection factories, this means that the object you register as a Resource must implement the javax.sql.DataSource interface.

There are more examples of DataSources for various databases here.

Configuring JMS Queues, Topics and ConnectionFactory-s

Jetty is able to bind any implementation of the JMS destinations and connection factories. You just need to ensure the implementation jars are available on Jetty's classpath.

Here's an example of binding an ActiveMQ in-JVM connection factory:

There is more information about ActiveMQ and Jetty here.

Configuring Mail

Jetty also provides infrastructure for providing access to javax.mail.Sessions from within an application:

The setup above creates an instance of the org.mortbay.naming.factories.MailSessionReference class, calls it's setter methods setUser("fred");, setPassword("OBF:1xmk1w261z0f1w1c1xmq"); to set up the authentication for the mail system, then populates a set of Properties, setting them on the MailSessionReference instance. The result of this is that an application can lookup java:comp/env/mail/Session at runtime and obtain access to a javax.mail.Session that has the necessary configuration to permit it to send email via SMTP.


You can set the password to be plain text, or use Jetty's password obfuscation mechanism to make the config file more secure from prying eyes. Note that the other Jetty encryption mechanisms of MD5 and Crypt cannot be used as the original password cannot be recovered, which is necessary for the mail system.

We will be adding more examples of configuring database datasources (eg using XAPool and DBCP) and jms connection factories, so check back regularly. Contributions are also welcome.

Configuring XA Transactions

If you want to be able to perform distributed transactions with your resources, you will need a transaction manager that supports the JTA interfaces that you can lookup as java:comp/UserTransaction in your webapp. Jetty does not ship with one, rather you may plug in the one of your preference. You can configure the one of your choice using the object in a jetty config file. In the following example, we will configure the Atomikos transaction manager:



In order to use the Atomikos transaction manager, you will need to download it and install it. There are instructions here on how to configure it for jetty6.

See also the instructions for how to configure JOTM. Contributions of instructions for other transaction managers are welcome.

Global or scoped to a webapp

If you place any of the naming resources discussed in the next sections in a jetty configuration file, they will be used by all webapps to resolve web.xml references.

You can instead make naming resources that are only resolvable by a particular webapp. For instance, you might want to do this if one of the objects you want to bind can only be loaded from the webapp's classloader.

Here's how to do it. Create a WEB-INF/jetty-env.xml file in your webapp. An example might look like:

That's all you need to do. Jetty will automatically check for a file of this name and add any naming resources in the appropriate scope for resolution of web.xml references. You may define any naming resources in this file, but it would usually be undesirable to define a Transaction, because this is intended to be a container-level resource.



Object bindings declared in jetty-env.xml will automatically be bound into the java:comp/env namespace. Therefore, if I declare an object with jndi name of "foo", it will be made available in jndi as "java:comp/env/foo".

Thus, if the object bindings you are putting in jetty-env.xml need to be able to refer to each other by jndi name, then you need to preface those names with "java:comp/env".

For example, two objects with names "A" and "B" are declared. "B" wants to know "A"'s jndi name to be able to look it up later. "B" must use "java:comp/env/A" and not just "A":

Demo Web Application

There is a demonstration webapp which sets up examples of all of the JNDI resources we've discussed so far.

In order to run this demonstration, you will need to download the transaction manager of your choice and Derby . At the time of writing, the webapp has been tested with both JOTM and with Atomikos transaction managers.

Building the Demo

As the demo webapp is not pre-built with the distribution, you first have to build it. It is located in examples/test-jndi-webapp. There is a README.txt file in there which explains how to build it, and how to add support for different transaction managers.

  • run "mvn clean install" to build it
  • then edit contexts/test-jndi.xml and uncomment one of the transaction manager setups
  • then edit contexts/test-jndi.d/WEB-INF/jetty-env.xml and uncomment one of the transaction manager setups
  • copy a derby.jar to the jetty lib/ directory, as well as copy all the necessary jars for the flavour of transaction manager you are using. There are instructions for some of the popular transaction managers on the wiki at JNDI

You run the demo like so:

The URL for the demonstration is at:

Adding Support for a Different Transaction Manager

  1. Edit the src/etc/templates/ file and add a new set of token and replacement strings following the pattern established for ATOMIKOS and JOTM.
  2. Edit the src/etc/templates/jetty-env.xml file and add configuration for new transaction manager following the pattern established for the other transaction managers.
  3. Edit the src/etc/templates/jetty-test-jndi.xml file and add configuration for the new transaction manager following the pattern established for the other transaction managers.
  • No labels
Contact the core Jetty developers at
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery