📄 catalogbean.java
字号:
package examples;import javax.ejb.*;import java.rmi.RemoteException;import java.util.*;import javax.naming.*;import javax.rmi.PortableRemoteObject;/** * This catalog Stateless Session Bean retrieves a list of Bookitems. * BookItem has Book Id, name of the Book and description */public class CatalogBean implements SessionBean { /* * Although this is a stateless session bean, we * do have state - the session context. Remember * that stateless session beans can store state, they * just can't store state on behalf of particular * clients. */ private SessionContext ctx; //---------------------------------------------------- // End EJB-required methods //---------------------------------------------------- /** * Returns the Bookitem for the given Book id */ public BookItem getBookItem(String bookId){ try{ Context ctx = new InitialContext(); BookHome bookHome = (BookHome) PortableRemoteObject.narrow( ctx.lookup("BookHome"), BookHome.class); Book book=bookHome.findByPrimaryKey(bookId); /* * Creates and returns the Bookitem */ return new BookItem(book.getBookID(),book.getName(), book.getBasePrice(),book.getDescription()); }catch (Exception e) { throw new EJBException(e); } } /** * Returns the list of Bookitems */ public Vector getBookItemList(){ try{ Context ctx = new InitialContext(); BookHome bookHome = (BookHome) PortableRemoteObject.narrow( ctx.lookup("BookHome"), BookHome.class); Collection books=bookHome.findAllBooks(); Enumeration items = Collections.enumeration(books); Vector bookItems=new Vector(); while (items.hasMoreElements()) { Book book= (Book) items.nextElement(); /* * Creates a new Bookitem */ BookItem p=new BookItem(book.getBookID(),book.getName(), book.getBasePrice(),book.getDescription()); bookItems.add(p); } /* * Returns the vector of Book items */ return bookItems; }catch (Exception e) { throw new EJBException(e); } } //---------------------------------------------------- // Begin EJB-required methods. The methods below are // called by the Container, and never called by client // code. //---------------------------------------------------- public void ejbCreate() throws RemoteException { System.out.println("ejbCreate() called."); } public void ejbRemove() { System.out.println("ejbRemove() called."); } public void ejbActivate() { System.out.println("ejbActivate() called."); } public void ejbPassivate() { System.out.println("ejbPassivate() called."); } /** * Associates this Bean instance with a particular * context. */ public void setSessionContext(SessionContext ctx) { System.out.println("setSessionContext() called"); this.ctx = ctx; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -