public interface ShoppingCart extends javax.ejb.EJBObject{ boolean addItem(int itemNumber) throws java.rmi.RemoteException; boolean purchase() throws Java.rmi.RemoteException; }
public class ShoppingCartEJB implements SessionBean{ public boolean addItem(int itemNumber){ // the code for adding items to the cart // may include JDBC code. } public boolean purchase(){ //the code for purchases } public ejbCreate(String accountName, String account){ // object initialization code } }
public interface CartHome extends Javax.ejb.EJBHome{ Cart create(String customerName , String account) throws RemoteException, BadAccountException; }
Context initialContext = new InitialContext(); CartHome cartHome = (CartHome) initialContext.lookup ("application/mall/shopping-carts");
ShoppingCart cart = cartHome.create("Emma","0507"); Cart.addItem(100); Cart.addItem(251); Cat.purchase();在上述代碼中, create()方法創建了一個新的session Bean, 變量cart 包括了一個對遠程EJB Object的引用, EJB Object允許你調用它的方法additem()和purchase()。cartHome中的create()方法將調用對應的Bean中的ejbCreate()方法。