user manual

Chapter 11: Writing enterprise bean clients 103
Client view of an enterprise bean
... //do the sort and merge work
sort.remove();
}
}
The main() routine of the client program throws the generic exception
Exception. When coded this way, the SortClient program does not have to
catch any exceptions that might occur, though if an exception occurs it will
terminate the program.
Obtaining the remote interface
Now that we have obtained the home interface of an enterprise bean we can
get a reference to the enterprise bean's remote interface. To do this, we use
the home interface's create or finder methods. The exact method to invoke
depends on the type of the enterprise bean and the methods the enterprise
bean provider has defined in the home interface.
For example, the first code sample shows how SortClient obtains a reference
to the Sort remote interface. Once SortClient obtains the reference to the
home interface and casts it to its proper type (SortHome), then the code can
create an instance of the bean and call its methods. It calls the home
interface's create() method, which returns a reference to the bean's remote
interface, Sort. (Because SortBean is a stateless session bean, its home
interface has only one create() method and that method by definition takes no
parameters.) SortClient can then call the methods defined on the remote
interface--sort() and merge()--to do its sorting work. When the work finishes,
the client calls the remote interface's remove() method to remove the instance
of the enterprise bean.
Session beans
A client obtains a reference to a session bean's remote interface by calling
one of the create methods on the home interface.
All session beans must have at least one create() method. A stateless session
bean must have only one create() method, and that method must have no
arguments. A stateful session bean can have one create() method, and may
have additional create() methods whose parameters vary. If a create() method
does have parameters, the values of these parameters are used to initialize
the session bean.
The default create() method has no parameters. For example, the sort
example uses a stateless session bean. It has, by definition, one create()
method that takes no parameters:
Sort sort = home.create();
The cart example, on the other hand, uses a stateful session bean, and its
home interface, CartHome, implements more than one create() method. One
of its create() methods takes three parameters, which together identify the
purchaser of the cart contents, and returns a reference to the Cart remote
interface. The CartClient sets values for the three parameters--cardHolderName,