Prev | Next

TOC | Index

J2EETM Developer's Guide
Session Beans


Passing a Session Bean's Object Reference

Suppose that your session bean needs to pass a reference to itself to another bean. You might want to pass the reference, for example, so that the second bean can call your session bean's methods. You can't pass the this reference because the session bean is not a remote object. Instead, your bean must pass an object reference for the instance. It gets the reference to itself by calling the getEJBObject method of the SessionContext interface. This interface provides a session bean with access to the instance context maintained by the EJB container. Typically, the bean saves the context in the setSessionContext method. The following code fragment shows how you might use these methods.

public class WagonEJB implements SessionBean {
   
   SessionContext context;
   . . .
   public void setSessionContext(SessionContext sc) { 
      this.context = sc; 
   }
   . . .
   public void passItOn(Basket basket) {
   . . .
      basket.copyItems(context.getEJBObject()); 
   } 
   . . .



Prev | Next

TOC | Index


Copyright © 2000 Sun Microsystems, Inc. All rights reserved.