Step #7:  Write the Enterprise JavaBean Client
  1. Overview of writing the client
  2. WebLogic demoClient.java
  3. Oracle client [PENDING]

Overview of writing the client

Writing the client is the other place you get to write some code, apart from the EJB bean business logic itself. The client to an EJB bean can be a variety of things, for example, a servlet, or an applet, or perhaps a C/C++ program. For our DemoBean EJB, we will use a simple java application called DemoClient.java. The things to note about this program are:

  • the calls to JNDI to find the home interface
  • the use of the home interface to get the container to create a new instance
  • the use of the remote interface to ask the container to execute the methods of the EJB bean.

One more thing to note is that as we deploy our bean into different containers/servers, there will need to be differing versions of the client code. The differences in client code are not expected to be major; instead, they will be issues, such as the strings for getting the initial connection. For example, consider the following code, which sets up the Properties object for WebLogic to retrieve the JNDI initialContext object. It may differ from the Oracle Properties string to get the initialContext object.

p.put(Context.INITIAL_CONTEXT_FACTORY,
          "weblogic.jndi.T3InitialContextFactory");

There are a variety of other small issues which may require some tuning and recompilation of the client code, but as stated, these are not expected to require huge amounts of work.

WebLogic demoClient.java

DemoClient.java (source)



/**
 * DemoClient -- demonstrates using a minimal java application
 *              to talk to the DemoBean stateless session bean
 */ 

package ejb.demo;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;

import examples.ejb.statelessSession.interfaces.*;

/**
 * DemoClient demonstrates using a minimal stateless session
 * bean.
 * Remember view session beans as an extension of your client
 * running in the server.
 */

public class demoClient { 
  public static void main(String[] args) { 
    System.out.println("\nBegin demoClient...\n"); 

    parseArgs(args); 
 
    try { 
      // Create A DemoBean object, in the server
      // Note: the name of the class corresponds to the JNDI
      // property declared in the DeploymentDescriptor
      // From DeploymentDescriptor ...
      // beanHomeName              demo.DemoHome
      Context ctx           = getInitialContext();
      DemoHome dhome = (DemoHome) ctx.lookup("demo.DemoHome");

      // Now we have a reference to the DemoHome object factory
      // use it to ask the container to creat an instance of
      // the Demo bean
      System.out.println("Creating Demo\n"); 
      Demo demo = dhome.create(); 
 
      // Here is the call that executes the method on the 
      // server side object
      System.out.println("The result is " + demo.demoSelect();)
 

    } 
    catch (Exception e) { 
      System.out.println(" => Error <= "); 
      e.printStackTrace(); 
    } 
    System.out.println("\nEnd demoClient...\n"); 
  } 

  static void parseArgs(String args[]) { 
    if ((args == null) || (args.length == 0)) 
      return; 
    for (int i = 0; i < args.length; i++) { 
      if (args[i].equals("-url")) 
        Integer.parseInt(args[++i]); 
      else if (args[i].equals("-user")) 
        user = args[++i]; 
      else if (args[i].equals("-password")) 
        password = args[++i]; 
    } 
  } 
 
  static String user     = null; 
  static String password = null; 
  static String url      = "t3://localhost:7001"; 

  /** 
   * Gets an initial context. 
   * 
   * @return                  Context 
   * @exception               java.lang.Exception if there is 
   *                          an error in getting a Context 
   */ 
  static public Context getInitialContext() throws Exception { 
    Properties p = new Properties(); 
    p.put(Context.INITIAL_CONTEXT_FACTORY, 
          "weblogic.jndi.T3InitialContextFactory"); 
    p.put(Context.PROVIDER_URL, url); 
    if (user != null) { 
      System.out.println ("user: " + user); 
      p.put(Context.SECURITY_PRINCIPAL, user); 
      if (password == null) 
        password = ""; 
      p.put(Context.SECURITY_CREDENTIALS, password); 
    } 
    return new InitialContext(p); 
  } 


Oracle demoClient.java

[PENDING]




previous next