O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required