This illustration demonstrates a servlet or Java client and an EJB client invoking an EJB 3.0 stateful session bean. In the EJB container, there is a remote component interface and bean instance. The remote component interface Cart
is just a plain old Java interface annotated with @Remote
. The bean instance CartBean
is just a plain old Java object annotated with @Stateful
. The bean instance annotates an optional method initialize
with @PostConstruct
to designate that method as the method the container invokes after it constructs an instance of this EJB. The bean instance annotates an optional method doneShopping
with @Remove
to designate that method as the method the container invokes to remove a given instance of the EJB. There is no home interface: the EJB container handles instantiation. The servlet or Java client uses JNDI to lookup an instance of Cart
by "java:comp/env/Cart
". The EJB client uses resource injection by annotating a Cart
instance variable with @EJB
. In both cases, the EJB container handles instantiation and no narrowing is required. Both clients can then use the Cart
interface to invoke methods on the CartBean
, such as addItem
. When either client is done, it invokes the Cart
method doneShopping
.