16.4. Accessing an EJB from a Servlet or JSP Page

Starting with JOnAS 2.6 with its web container service, it is possible to access an enterprise Java bean and its environment in a J2EE-compliant way.

The following sections describe:

  1. How to access the Remote Home interface of a bean.

  2. How to access the Local Home interface of a bean.

  3. How to access the environment of a bean.

  4. How to start transactions in servlets.

NoteNote
 

All the following code examples are taken from the The EarSample example provided in the JOnAS distribution.

16.4.1. Accessing the Remote Home Interface of a Bean:

In this example the servlet gets the Remote Home interface OpHome registered in JNDI using an EJB reference, then creates a new instance of the Session Bean:

import javax.naming.Context;
import javax.naming.InitialContext;

//remote interface
import org.objectweb.earsample.beans.secusb.Op;
import org.objectweb.earsample.beans.secusb.OpHome;

  Context initialContext = null;
  try {
    initialContext = new InitialContext();
  } catch (Exception e) {
    out.print("<li>Cannot get initial context for JNDI: ");
    out.println(e + "</li>");
    return;
  }
// Connecting to OpHome through JNDI
  OpHome opHome = null;
  try {
    opHome = (OpHome) 
    PortableRemoteObject.narrow(initialContext.lookup \
      ("java:comp/env/ejb/Op"), OpHome.class);
  } catch (Exception e) {
    out.println("<li>Cannot lookup java:comp/env/ejb/Op: " 
       + e + "</li>");
    return;
  }
  // OpBean creation
  Op op = null;
  try {       
    op = opHome.create("User1"); 
  } catch (Exception e) {
    out.println("<li>Cannot create OpBean: " + e + "</li>");
    return;
  }

Note that the following elements must be set in the web.xml file tied to this web application:

<ejb-ref>
  <ejb-ref-name>ejb/Op</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <home>org.objectweb.earsample.beans.secusb.OpHome</home>
  <remote>org.objectweb.earsample.beans.secusb.Op</remote>
  <ejb-link>secusb.jar#Op</ejb-link>
</ejb-ref>

16.4.2. Accessing the Local Home of a Bean:

The following example shows how to obtain a local home interface OpLocalHome using an EJB local reference:

//local interfaces
import org.objectweb.earsample.beans.secusb.OpLocal;
import org.objectweb.earsample.beans.secusb.OpLocalHome;

  // Connecting to OpLocalHome thru JNDI
    OpLocalHome opLocalHome = null;
    try {
      opLocalHome = (OpLocalHome) 
          initialContext.lookup("java:comp/env/ejb/OpLocal");
    } catch (Exception e) {
      out.println("<li>Cannot lookup java:comp/env/ejb/OpLocal: " 
          + e + "</li>");
      return;
    }

This is found in the web.xml file:

<ejb-local-ref>
  <ejb-ref-name>ejb/OpLocal</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <local-home>org.objectweb.earsample.beans.secusb.OpLocalHome
    </local-home>
  <local>org.objectweb.earsample.beans.secusb.OpLocal</local>
  <ejb-link>secusb.jar#Op</ejb-link>
</ejb-local-ref>

16.4.3. Accessing the Environment of the Component

In this example, the servlet seeks to access the component's environment:

String envEntry = null;
  try {
    envEntry = (String) 
      initialContext.lookup("java:comp/env/envEntryString");
  } catch (Exception e) {
    out.println("<li>Cannot get env-entry on JNDI " + e + "</li>");
    return;
  }

This is the corresponding part of the web.xml file:

<env-entry>
  <env-entry-name>envEntryString</env-entry-name>
  <env-entry-value>This is a string from env-entry</env-entry-value>
  <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

16.4.4. Starting Transactions in Servlets

The servlet wants to start transactions via the UserTransaction:

import javax.transaction.UserTransaction;

    // We want to start transactions from client: get UserTransaction
     UserTransaction utx = null;
     try {
         utx = (UserTransaction) 
         initialContext.lookup("java:comp/UserTransaction");
     } catch (Exception e) {
         out.println("<li>Cannot lookup java:comp/UserTransaction: " 
         + e + "</li>");
         return;
     }

     try { 
         utx.begin();
         opLocal.buy(10);
         opLocal.buy(20); 
         utx.commit();

     } catch (Exception e) {
         out.println("<li>exception during 1st Tx: " + e + "</li>");
         return;
     }