[ Team LiB ] Previous Section Next Section

Recipe 25.1 Configuring a JNDI Object in Tomcat

Problem

You want to configure a JavaBean as a JNDI object using Tomcat 4.

Solution

Create Resource and ResourceParam elements in server.xml or in the XML file that represents your web application (located in Tomcat's webapps folder). Then add a resource-env-ref element to web.xml.

Discussion

The JNDI object for Tomcat is set up in conf/server.xml. If you have configured a web application as a separate XML file in Tomcat's webapps folder, then configure the JNDI resource in this XML file instead. Example 25-1 shows the set up for binding a JavaBean as a JNDI object. The bean is named com.jspservletcookbook.StockPriceBean.

Example 25-1. The server.xml element for configuring a JNDI object
<Resource name="bean/pricebean" type=
  "com.jspservletcookbook.StockPriceBean" auth="Container" description=
  "A web harvesting bean"/>

<ResourceParams name="bean/pricebean">

    <parameter>
        <name>factory</name>
        <value>org.apache.naming.factory.BeanFactory</value>
    </parameter>

</ResourceParams>

Example 25-1 includes a Resource element and a ResourceParams element that references the Resource by name ("bean/pricebean"). This name is the address by which Java code accesses a bean instance using the JNDI API.

Example 25-2 shows the resource-env-ref element that must appear in the deployment descriptor (web.xml) in order for web application code to access the JNDI object. Store the com.jspservletcookbook.StockPriceBean class in WEB-INF/classes or in a JAR file placed in WEB-INF/lib.

Example 25-2. Place this element in the deployment descriptor web.xml
<!-- start of deployment descriptor -->

<resource-env-ref>

    <description>
     A factory for StockPriceBean
    </description>

        <resource-env-ref-name>
          bean/pricebean
          </resource-env-ref-name>

        <resource-env-ref-type>
          com.jspservletcookbook.StockPriceBean
          </resource-env-ref-type>

</resource-env-ref>

<!-- rest of deployment descriptor -->

Example 25-3 shows a snippet of code that uses the JNDI API, just to start you on how the configuration fits in with JNDI-related code.

Example 25-3. Code snippet for accessing a Tomcat JNDI resource
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

//This code may appear in a servlet's init( ) method or perhaps
//in doGet( ) or doPost( )

Context env = null;

StockPriceBean spbean = null;
        
 try{
           
     env = (Context) new InitialContext( ).lookup("java:comp/env");

     spbean = (StockPriceBean) env.lookup("bean/pricebean");

     if (spbean == null)
         throw new ServletException(
         "bean/pricebean is an unknown JNDI object");

     //close the InitialContext
     env.close( );
             
 } catch (NamingException ne) { 
        
     //close the Context if you're not using it again
     try{ env.close( ); } catch(NamingException nex) {}

     throw new ServletException(ne);
}

Example 25-3 imports the necessary classes from the javax.naming package. Then two lookups take place to get a reference to a JavaBean that has been bound to a JNDI implementation. The first lookup provides the initial context:

env = (Context) new InitialContext( ).lookup("java:comp/env");

The second lookup attempts to return a StockPriceBean object:

spbean = (StockPriceBean) env.lookup("bean/pricebean");

The code closes the InitialContext to release the object's resources, if the code is not going to use the context again for another lookup. The next recipe uses code like this from servlets and JSPs.

See Also

Recipe 25.2 on accessing the Tomcat JNDI object from a servlet; Recipe 25.3 on accessing the Tomcat JNDI object from a JSP; Chapter 21 on accessing DataSources with JNDI.

    [ Team LiB ] Previous Section Next Section