Session Beans
一个session bean代表了一个单独的客户会话。客户端调用session bean的方法,session bean 完成客户端的请求。
从名字上可以看出,session bean类似于一个交互的会话。session bean不能共享,一个session bean只能有一个客户端, 就像是一个交互式的会话只能有一个用户一样。像交互式会话一样,session bean不是永远存在的,当客户终止,session bean也停止而且不在和客户发生联系。
来看一个session bean的例子:
这个session bean是一个在线书店的例子。客户可以添加或者删除一本书,或者浏览已经定购的内容。在这个例子里,一共定义了5个类:
Session Bean Class (CartEJB)
Home Interface (CartHome)
Remote Interface (Cart)
BookException
IVerifier
CartClient
Cart.java:
import java.util.*;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Cart extends EJBObject {
public void addBook(String title) throws RemoteException;
public void removeBook(String title) throws BookException,
RemoteException;
public Vector getContents() throws RemoteException;
}
CartEJB.java:
import java.util.*;
import javax.ejb.*;
public class CartEJB implements SessionBean {
String customerName;
String customerId;
Vector contents;
public void ejbCreate(String person) throws CreateException {
if (person == null) {
throw new CreateException("Null person not allowed.");
}
else {
customerName = person;
}
customerId = "0";
contents = new Vector();
}
public void ejbCreate(String person, String id)
throws CreateException {
if (person == null) {
throw new CreateException("Null person not allowed.");
}
else {
customerName = person;
}
IdVerifier idChecker = new IdVerifier();
if (idChecker.validate(id)) {
customerId = id;
}
else {
throw new CreateException("Invalid id: " + id);
}
contents = new Vector();
}
public void addBook(String title) {
contents.addElement(title);
}
public void removeBook(String title) throws BookException {
boolean result = contents.removeElement(title);
if (result == false) {
throw new BookException(title + " not in cart.");
}
}
public Vector getContents() {
return contents;
}
public CartEJB() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
CartHome.java:
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface CartHome extends EJBHome {
Cart create(String person) throws RemoteException,
CreateException;
Cart create(String person, String id) throws
RemoteException,
CreateException;
}
BookException.java:
public class BookException extends Exception {
public BookException() {
}
public BookException(String msg) {
super(msg);
}
}
IdVerifier.java:
public class IdVerifier {
public IdVerifier() {
}
public boolean validate(String id) {
boolean result = true;
for (int i = 0; i < id.length(); i++) {
if (Character.isDigit(id.charAt(i)) == false)
result = false;
}
return result;
}
}
CartClient.java:
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class CartClient {
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("java:comp/env/ejb/SimpleCart");
CartHome home =
(CartHome)PortableRemoteObject.narrow(objref,
CartHome.class);
Cart shoppingCart = home.create("Duke DeEarl","123");
shoppingCart.addBook("The Martian Chronicles");
shoppingCart.addBook("2001 A Space Odyssey");
shoppingCart.addBook("The Left Hand of Darkness");
Vector bookList = new Vector();
bookList = shoppingCart.getContents();
Enumeration enumer = bookList.elements();
while (enumer.hasMoreElements()) {
String title = (String) enumer.nextElement();
System.out.println(title);
}
shoppingCart.removeBook("Alice in Wonderland");
shoppingCart.remove();
} catch (BookException ex) {
System.err.println("Caught a BookException:
" + ex.getMessage());
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
如果您对本文有任何疑问或者建议,请到讨论区发表您的意见:
>>
论坛入口 <<
上一篇:EJB和JSP, Servlet(一) EJB简介(补充)
下一篇:EJB和JSP, Servlet(一) EJB简介
【文章评论】
【收藏本文】
【推荐好友】
【打印本文】
【我要投稿】 【论坛讨论】
更多相关文章
|
|