Writing a Client

Listing 5.8 demonstrates how a client accesses a stateless session bean.

Listing 5.8. The Full Text of day05/Client.java
package day05;

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

public class Client {

  public static void main(String[] args) {
    print("Starting Client . . .
");
    Context initialContext = null;
    SignOnHome signOnHome = null;
    SignOn signOn = null;

    try {
       print("Looking up the sign on component via JNDI.
");
       initialContext = new InitialContext();
       Object object = initialContext.lookup("day05/SignOn");
       signOnHome = (SignOnHome)
        javax.rmi.PortableRemoteObject.narrow(object,SignOnHome.class);

       print("Creating an signOn object.
");
       signOn =  (SignOn) signOnHome.create();

       print("Testing a successful login/password
");
       signOn.validateUser("student", "password");

       print("Testing an invalid login/password
");
       try {
          signOn.validateUser("student", "invalidpassword");
       } catch(InvalidLoginException ile) {
          System.err.println(ile);
       }
       print("Removing the signOn object.
");
       signOn.remove();
    } catch ( Exception e) {
       e.printStackTrace();
    }
  }
   static void print(String s) {
     System.out.println(s);
  }
}

The client locates the SignOnHome home interface of the deployed enterprise bean via JNDI, and then uses the remote home interface to create a remote SignOn session object. The client then calls the validateUser() business method with a valid login name and password on the remote object. It also demonstrates the application-specific exception by calling the validateUser() method with an invalid login name and password. Finally, the client calls the remove() method on the remote interface. The container will destroy the remote session object.

You can build the client in the same command window you used for packaging the enterprise bean by using the following command:

C:styejbexamplesday05>javac -g -classpath %CLASSPATH%;.uild -d build Client.java
					

For convenience, we provide a single script, buildWebLogic.bat, under the c:styejbexamplesday05 directory to package and deploy the enterprise bean. It also builds the client.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.189.251