Jetty Logo
Version: 9.4.26-SNAPSHOT
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 for sponsored feature development

Using JNDI with Jetty Embedded

Setting up the Classpath
Example Code

Setting up the Classpath

In addition to the jars that you require for your application, and the jars needed for core Jetty, you will need to place the following jars onto your classpath:


If you are using transactions, you will also need the javax.transaction api. You can obtain this jar here.

If you wish to use mail, you will also need the javax.mail api and implementation which you can download here. Note that this jar also requires the javax.activation classes, which is available at this link.

Example Code

Here is an example class that sets up some JNDI entries and deploys a webapp that references these JNDI entries in code. We’ll use some mocked up classes for the transaction manager and the DataSource in this example for simplicity:

import java.util.Properties;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

 * ServerWithJNDI
public class ServerWithJNDI
    public static void main(String[] args) throws Exception

        //Create the server
        Server server = new Server(8080);

        //Enable parsing of jndi-related parts of web.xml and jetty-env.xml
        org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
        classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "", "");

        //Create a WebApp
        WebAppContext webapp = new WebAppContext();

        //Register new transaction manager in JNDI
        //At runtime, the webapp accesses this as java:comp/UserTransaction transactionMgr = new com.acme.MockUserTransaction());

        //Define an env entry with Server scope.
        //At runtime, the webapp accesses this as java:comp/env/woggle
        //This is equivalent to putting an env-entry in web.xml:
        //  <env-entry-name>woggle</env-entry-name>
        //  <env-entry-type>java.lang.Integer</env-entry-type>
        //  <env-entry-value>4000</env-entry-value>
        //</env-entry> woggle = new, "woggle", new Integer(4000), false);

        //Define an env entry with webapp scope.
        //At runtime, the webapp accesses this as java:comp/env/wiggle
        //This is equivalent to putting a web.xml entry in web.xml:
        //  <env-entry-name>wiggle</env-entry-name>
        //  <env-entry-value>100</env-entry-value>
        //  <env-entry-type>java.lang.Double</env-entry-type>
        //Note that the last arg of "true" means that this definition for "wiggle" would override an entry of the
        //same name in web.xml wiggle = new, "wiggle", new Double(100), true);

        //Register a reference to a mail service scoped to the webapp.
        //This must be linked to the webapp by an entry in web.xml:
        // <resource-ref>
        //  <res-ref-name>mail/Session</res-ref-name>
        //  <res-type>javax.mail.Session</res-type>
        //  <res-auth>Container</res-auth>
        // </resource-ref>
        //At runtime the webapp accesses this as java:comp/env/mail/Session
        org.eclipse.jetty.jndi.factories.MailSessionReference mailref = new org.eclipse.jetty.jndi.factories.MailSessionReference();
        Properties props = new Properties();
        props.put("mail.smtp.auth", "false");
        props.put("mail.debug", "false");
        mailref.setProperties(props); xxxmail = new, "mail/Session", mailref);

        // Register a  mock DataSource scoped to the webapp
        //This must be linked to the webapp via an entry in web.xml:
        //  <res-ref-name>jdbc/mydatasource</res-ref-name>
        //  <res-type>javax.sql.DataSource</res-type>
        //  <res-auth>Container</res-auth>
        //At runtime the webapp accesses this as java:comp/env/jdbc/mydatasource mydatasource = new, "jdbc/mydatasource",
                                                                                                     new com.acme.MockDataSource());


See an error or something missing? Contribute to this documentation at Github!(Generated: 2020-01-17)