|  | <html><head> | 
|  | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|  | <title>Working with Jetty JNDI</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="jndi.html" title="Chapter 11. Configuring JNDI"><link rel="prev" href="jndi.html" title="Chapter 11. Configuring JNDI"><link rel="next" href="jndi-configuration.html" title="Configuring JNDI"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small"> | 
|  | Version: 9.3.0.v20150612</span></td><td style="width: 50%"><script type="text/javascript">  (function() { | 
|  | var cx = '016459005284625897022:obd4lsai2ds'; | 
|  | var gcse = document.createElement('script'); | 
|  | gcse.type = 'text/javascript'; | 
|  | gcse.async = true; | 
|  | gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + | 
|  | '//www.google.com/cse/cse.js?cx=' + cx; | 
|  | var s = document.getElementsByTagName('script')[0]; | 
|  | s.parentNode.insertBefore(gcse, s); | 
|  | })(); | 
|  | </script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Working with Jetty JNDI</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jndi.html"><i class="icon-chevron-left"></i> Previous</a> </td><th width="60%" align="center">Chapter 11. Configuring JNDI<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="jndi-configuration.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at | 
|  | <span class="website">www.webtide.com</span></a></h5><p> | 
|  | 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 | 
|  | </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="using-jetty-jndi"></a>Working with Jetty JNDI</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="using-jetty-jndi.html#d0e9947">Defining the web.xml</a></span></dt><dt><span class="section"><a href="using-jetty-jndi.html#defining-jndi-naming-entries">Declaring Resources</a></span></dt><dt><span class="section"><a href="using-jetty-jndi.html#jndi-where-to-declare">Deciding Where to Declare Resources</a></span></dt><dt><span class="section"><a href="using-jetty-jndi.html#jndi-name-scope">Scope of Resource Names</a></span></dt><dt><span class="section"><a href="using-jetty-jndi.html#binding-objects-into-jetty-jndi">What Can Be Bound as a Resource?</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e9947"></a>Defining the web.xml</h3></div></div></div><p>You can configure naming resources to reference in a | 
|  | <code class="filename">web.xml</code> file and access from within the | 
|  | <code class="code">java:comp/env</code> naming environment of the webapp during | 
|  | execution. Specifically, you can configure support for the following | 
|  | <code class="filename">web.xml</code> elements:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> | 
|  | <![CDATA[ | 
|  |  | 
|  | <env-entry/> | 
|  | <resource-ref/> | 
|  | <resource-env-ref/> | 
|  |  | 
|  | ]]> | 
|  | </script></div><p><a class="link" href="jndi-configuration.html#configuring-jndi-env-entries" title="Configuring JNDI env-entries">Configuring | 
|  | env-entries</a> shows you how to set up overrides for <code class="code"> | 
|  | env-entry</code> elements in <code class="filename">web.xml</code>, while <a class="link" href="jndi-configuration.html#configuring-resource-refs-and-resource-env-refs" title="Configuring resource-refs and resource-env-refs">Configuring | 
|  | <code class="code">resource-refs</code> and <code class="code">resource-env-refs</code></a> discusses how to configure | 
|  | support resources such as <code class="code">javax.sql.DataSource</code>.</p><p>You can also plug a JTA | 
|  | <code class="code">javax.transaction.UserTransaction</code> implementation into Jetty | 
|  | so that webapps can look up <code class="code">java:comp/UserTransaction</code> to | 
|  | obtain a distributed transaction manager: see <a class="link" href="jndi-configuration.html#configuring-xa-transactions" title="Configuring XA Transactions">Configuring XA | 
|  | Transactions</a>.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="defining-jndi-naming-entries"></a>Declaring Resources</h3></div></div></div><p>You must declare the objects you want bound into the Jetty | 
|  | environment so that you can then hook into your webapp via <code class="code">env-entry, | 
|  | resource-ref</code> and <code class="code">resource-env-refs</code> in <code class="filename">web.xml</code>. You create these bindings | 
|  | by using declarations of the following types:</p><div class="variablelist"><dl><dt><span class="term"><code class="code">org.eclipse.jetty.plus.jndi.EnvEntry</code></span></dt><dd><p>for <code class="code">env-entry</code> type of entries</p></dd><dt><span class="term"><code class="code">org.eclipse.jetty.plus.jndi.Resource</code></span></dt><dd><p>for all other type of resources</p></dd><dt><span class="term"><code class="code">org.eclipse.jetty.plus.jndi.Transaction</code></span></dt><dd><p>for a JTA manager</p></dd><dt><span class="term"><code class="code">org.eclipse.jetty.plus.jndi.Link</code></span></dt><dd><p>for link between a <code class="filename">web.xml</code> resource name and a naming entry</p></dd></dl></div><p>Declarations of each of these types follow the same general pattern:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> | 
|  | <![CDATA[ | 
|  |  | 
|  | <New class="org.eclipse.jetty.plus.jndi.xxxx"> | 
|  | <Arg><!-- scope --></Arg> | 
|  | <Arg><!-- name --></Arg> | 
|  | <Arg><!-- value --></Arg> | 
|  | </New> | 
|  |  | 
|  | ]]> | 
|  | </script></div><p>You can place these declarations into three different | 
|  | files, depending on your needs and the <a class="link" href="using-jetty-jndi.html#jndi-name-scope" title="Scope of Resource Names">scope</a> of the resources being | 
|  | declared.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="jndi-where-to-declare"></a>Deciding Where to Declare Resources</h3></div></div></div><p>You can define naming resources in three places:</p><div class="variablelist"><dl><dt><span class="term">jetty.xml</span></dt><dd><p>Naming resources defined in a | 
|  | <code class="filename">jetty.xml</code> file are <a class="link" href="using-jetty-jndi.html#jndi-name-scope" title="Scope of Resource Names">scoped</a> at either the JVM level | 
|  | or the Server level. The classes for the resource must | 
|  | be visible at the Jetty container level. If the classes for the | 
|  | resource only exist inside your webapp, you must declare it in a | 
|  | <code class="filename">WEB-INF/jetty-env.xml</code> file.</p></dd><dt><span class="term">WEB-INF/jetty-env.xml</span></dt><dd><p>Naming resources in a | 
|  | <code class="filename">WEB-INF/jetty-env.xml</code> file are <a class="link" href="using-jetty-jndi.html#jndi-name-scope" title="Scope of Resource Names">scoped</a> to the web app in which | 
|  | the file resides. While you can enter JVM or Server scopes if | 
|  | you choose, we do not recommend doing so. The resources defined | 
|  | here may use classes from inside your webapp. This is | 
|  | a Jetty-specific mechanism.</p></dd><dt><span class="term">context xml file</span></dt><dd><p>Entries in a context xml file should be <a class="link" href="using-jetty-jndi.html#jndi-name-scope" title="Scope of Resource Names">scoped</a> at the level of the | 
|  | webapp to which they apply, although you can supply a less | 
|  | strict scoping level of Server or JVM if you choose. As with | 
|  | resources declared in a <code class="filename">jetty.xml</code> file, classes associated with | 
|  | the resource must be visible on the container's classpath.</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="jndi-name-scope"></a>Scope of Resource Names</h3></div></div></div><p>Naming resources within Jetty belong to one of three different | 
|  | scopes, in increasing order of restrictiveness:</p><div class="variablelist"><dl><dt><span class="term">JVM scope</span></dt><dd><p>The name is unique across the JVM instance, and is | 
|  | visible to all application code. You represent this scope by a | 
|  | <code class="code">null</code> first parameter to the resource declaration. | 
|  | For example:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> | 
|  | <![CDATA[ | 
|  |  | 
|  | <New id="cf" class="org.eclipse.jetty.plus.jndi.Resource"> | 
|  | <Arg></Arg>  <!-- empty arg --> | 
|  | <Arg>jms/connectionFactory</Arg> | 
|  | <Arg> | 
|  | <New class="org.apache.activemq.ActiveMQConnectionFactory"> | 
|  | <Arg>vm://localhost?broker.persistent=false</Arg> | 
|  | </New> | 
|  | </Arg> | 
|  | </New> | 
|  |  | 
|  | ]]> | 
|  | </script></div></dd><dt><span class="term">Server scope</span></dt><dd><p>The name is unique to a Server instance, and is only | 
|  | visible to code associated with that instance. You represent this scope by referencing the Server instance as the first | 
|  | parameter to the resource declaration. For example:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> | 
|  | <![CDATA[ | 
|  |  | 
|  | <Configure id="Server" class="org.eclipse.jetty.Server"> | 
|  | <New id="cf" class="org.eclipse.jetty.plus.jndi.Resource"> | 
|  | <Arg><Ref refid="Server"/></Arg>  <!-- reference to Server instance --> | 
|  | <Arg>jms/connectionFactory</Arg> | 
|  | <Arg> | 
|  | <New class="org.apache.activemq.ActiveMQConnectionFactory"> | 
|  | <Arg>vm://localhost?broker.persistent=false</Arg> | 
|  | </New> | 
|  | </Arg> | 
|  | </New> | 
|  | </Configure> | 
|  |  | 
|  | ]]> | 
|  | </script></div></dd><dt><span class="term">Webapp scope</span></dt><dd><p>The name is unique to the WebAppContext instance, and | 
|  | is only visible to code associated with that instance. You represent this scope by referencing the WebAppContext instance | 
|  | as the first parameter to the resource declaration. | 
|  | For example:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> | 
|  | <![CDATA[ | 
|  |  | 
|  | <Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext"> | 
|  | <New id="cf" class="org.eclipse.jetty.plus.jndi.Resource"> | 
|  | <Arg><Ref refid='wac'/></Arg> <!-- reference to WebAppContext --> | 
|  | <Arg>jms/connectionFactory</Arg> | 
|  | <Arg> | 
|  | <New class="org.apache.activemq.ActiveMQConnectionFactory"> | 
|  | <Arg>vm://localhost?broker.persistent=false</Arg> | 
|  | </New> | 
|  | </Arg> | 
|  | </New> | 
|  | </Configure> | 
|  |  | 
|  | ]]> | 
|  | </script></div></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="binding-objects-into-jetty-jndi"></a>What Can Be Bound as a Resource?</h3></div></div></div><p>You can bind four types of objects into a Jetty JNDI reference:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>An ordinary POJO instance.</p></li><li class="listitem"><p>A <a class="link" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/naming/Reference.html" target="_top"> | 
|  | javax.naming.Reference</a> instance.</p></li><li class="listitem"><p>An object instance that implements the <a class="link" href="http://docs.oracle.com/javase/1.5.0/docs/api/javax/naming/Referenceable.html" target="_top"> | 
|  | javax.naming.Referenceable</a> interface.</p></li><li class="listitem"><p>A link between a name as referenced in | 
|  | <code class="filename">web.xml</code> and as referenced in the Jetty | 
|  | environment.</p></li></ul></div></div></div><script type="text/javascript"> | 
|  | SyntaxHighlighter.all() | 
|  | </script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jndi.html"><i class="icon-chevron-left"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="jndi.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="jndi-configuration.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 11. Configuring JNDI </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top"> Configuring JNDI</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout"> | 
|  | See an error or something missing? | 
|  | <span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at | 
|  | <span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-06-15T13:18:21-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript"> | 
|  | var _gaq = _gaq || []; | 
|  | _gaq.push(['_setAccount', 'UA-1149868-7']); | 
|  | _gaq.push(['_trackPageview']); | 
|  |  | 
|  | (function() { | 
|  | var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; | 
|  | ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; | 
|  | var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); | 
|  | })(); | 
|  | </script></body></html> |