InfoCenter Home >
4: Developing applications >
4.7: Java Clients >
4.7.3: Java thin application client programming model >
4.7.3.2: Java thin application client code example

4.7.3.2: Java thin application client code example

The code required by a Java application thin client to communicate with an enterprise java bean is similar to servlet code that communicates with enterprise java beans.

The following code example illustrates how a Java application thin client uses the InitialContext to do the following:

  • Perform a lookup
  • Narrow the returned object into the EJBHome object
  • Invoke the create method.

Click a link to view the referenced line of code in the example. Each line in the code snippet is described in this next section.

  1. The first three lines in the try section of the code example show how to:
  2. The fields in the provider URL represent:
    iiop://myComputer.myDomain.com:900
    iiop:// myComputer myDomain.com 900
    protocol   name of the server where WebSphere Application Server is installed name of the domain for the server where WebSphere Application Server is installed
    configured port
      Since port 900 is the default port value, this may be omitted.
  3. This line in the example shows how to:
  4. Now do a lookup the EJB Home on the server

    For more information on JNDI, see article 4.6.1: JNDI overview.

  5. The narrow operation in this line:
  6. Finally, call the create method on the HelloHome object to create a Hello object.

    You can also use findByPrimary key instead of create. Use the findByPrimaryKey method to find an existing Hello object.

Code example


import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
import WebSphereSample.HelloEJB.*; //package for HelloEJB beans
public class HelloClient
{
public static void main(String argv[])
{
try
{
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
props.put(Context.PROVIDER_URL,
"iiop://myComputer.myDomain.com:900");
InitialContext ctx = new InitialContext(props);
Object myObj = ctx.lookup("WSsamples/HelloEJBHome");
HelloHome myHome = (HelloHome)
javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);
Hello hello = myHome.create();
}
catch(NamingException e)
....
catch(RemoteException e)
....
catch(CreateException e)
....
}
}

Learn more about the WebSphere Java application thin client by running the client sample. You can install the client sample from the WebSphere Application Client CD. This sample is called HelloEJB and is installed in the product_installation/WSsamples/Client subdirectory on the client machine.

Go to previous article: Developing a Java  application thin client Go to next article: Quick reference to Java client functions

 

 
Go to previous article: Developing a Java  application thin client Go to next article: Quick reference to Java client functions