You can use a context to grab a simple JavaBean instance. First create the POJO in your src/ directory:

package org.denevell.tomcat;
 public class MyBean {
   private String foo = "Default Foo";
   public String getFoo() {
    return this.foo;
 public void setFoo(String foo) {
    this.foo = foo;

Then tell your web.xml that you can get this via JNDI:

 <web-app ... >

The resource-env-ref-name gives your factory name that will be used later. The resource-env-ref-type tells it what class to to return.

You also need to define the factory in the Tomcat specific web/META-INF/context.xml:

    <Resource name="bean/MyBeanFactory"
            foo="From context.xml"/>

It first refers to the name above, and defines its class again, tells Tomcat what factory to use to create this resource and sets an initial parameter, calling getFoo() on the object in this case. This resource is by default a singleton.

You can now get an instance of that Java bean as follows:

  try {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
    writer.println("foo = " + bean.getFoo());
  } catch(NamingException e) {
    writer.println("Naming exception.");

We first get a InitialContext(), and get the environmental Context from such, ‘java:comp/env’ being the entry point to your servlet’s JNDI environment. Then we use that to refer to the factory using the JNDI name we defined. You can now play with the object to your heart’s content.

Tags: tomcat | tomcat-jndi | java
Posted: Sunday 10 February 2013, 6:30PM

Edit on Github!
comments powered by Disqus