9,修改方法ejbCreate(),setSessioncontext(),addItem(),removeItem()方法,修改後完整代碼如下:
/**
*
*/
package j2ee;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import java.util.*;
/**
*
* <!-- begin-user-doc -->
* A generated session bean
* <!-- end-user-doc -->
* *
* <!-- begin-xdoclet-definition -->
* @ejb.bean name="Cart"
* description="An EJB named Cart"
* display-name="Cart"
* jndi-name="Cart"
* type="Stateful"
* transaction-type="Container"
*
* <!-- end-xdoclet-definition -->
* @generated
*/
public abstract class CartBean implements javax.ejb.SessionBean {
/**
*
* <!-- begin-xdoclet-definition -->
* @ejb.create-method view-type="remote"
* <!-- end-xdoclet-definition -->
* @generated
*
* //TODO: Must provide implementation for bean create stub *
*/
SessionContext sessionContext;
String cardHolderName;
Vector items = new Vector();
String creditCardNumber;
public void ejbCreate(String cardHolderName, String creditCardNumber) {
this.cardHolderName = cardHolderName;
this.creditCardNumber = creditCardNumber;
}
/**
*
* <!-- begin-xdoclet-definition -->
* @ejb.interface-method view-type="remote"
* <!-- end-xdoclet-definition -->
* @generated
*
* //TODO: Must provide implementation for bean method stub
*/
public String foo(String param) {
return null;
}
/* (非 Javadoc)
* @see javax.ejb.SessionBean#ejbActivate()
*/
public void ejbActivate() throws EJBException, RemoteException {
// TODO 自動生成方法存根
}
/* (非 Javadoc)
* @see javax.ejb.SessionBean#ejbPassivate()
*/
public void ejbPassivate() throws EJBException, RemoteException {
// TODO 自動生成方法存根
}
/* (非 Javadoc)
* @see javax.ejb.SessionBean#ejbRemove()
*/
public void ejbRemove() throws EJBException, RemoteException {
// TODO 自動生成方法存根
}
/* (非 Javadoc)
* @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
*/
public void setSessionContext(SessionContext sessionContext)
throws EJBException,
RemoteException {
// TODO 自動生成方法存根
this.sessionContext = sessionContext;
}
/**
*
*/
public CartBean() {
// TODO 自動生成構造函數存根
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public void addItem(Item item) {
// TODO Auto-generated method stub
items.addElement(item);
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public void removeItem(Item item) {
// TODO Auto-generated method stub
Enumeration elements = items.elements();
while (elements.hasMoreElements()) {
Item current = (Item) elements.nextElement();
if (item.getClass().equals(current.getClass())
&& item.getTitle().equals(current.getTitle())) {
items.remove(current);
return;
}
}
}
/**
* Business method
* @ejb.interface-method view-type = "remote"
*/
public float getTotalPrice() {
// TODO Auto-generated method stub
float totalPrice=
Enumeration elements=items.elements();
while (elements.hasMoreElements()) {
Item current=(Item)elements.nextElement();
totalPrice+=current.getPrice();
}
return (long)(totalPrice*100)/
}
}
10,打開CartHome.java,CartLocalHome.java,將其中create()方法修改爲create(String cardHolderName, String creditCardNumber)
11,編寫客戶端測試程序。新建EJB Client Test,名爲:EjbTest如圖1-7
12,具體設置如下圖1-8
13,打開EjbTest.java,在文件末尾添加main()方法。具體代碼如下:
package j2ee;
import junit.framework.TestCase;
import java.util.Hashtable;
import javax.rmi.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.*;
/**
* EJB Test Client
*/
public class EjbTest extends TestCase {
public EjbTest() {
// TODO 自動生成構造函數存根
}
/** Home interface */
protected j2ee.CartHome home;
/**
* Get the initial naming context
*/
protected Context getInitialContext() throws Exception {
Hashtable props = new Hashtable();
props.put(
Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(
Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
Context ctx = new InitialContext(props);
return ctx;
}
/**
* Get the home interface
*/
protected j2ee.CartHome getHome() throws Exception {
Context ctx = this.getInitialContext();
Object o = ctx.lookup("Cart");
j2ee.CartHome intf = (j2ee.CartHome) PortableRemoteObject.narrow(
o,
j2ee.CartHome.class);
return intf;
}
/**
* Set up the test case
*/
protected void setUp() throws Exception {
this.home = this.getHome();
}
public static void main(String[] args){
Cart bean=null;
EjbTest client=new EjbTest();
float result=
try {
bean=client.getHome().create("Benson", "1234");
Item item1=new Item("Master EJB",
Item item2=new Item("Master JSP",
Item item3=new Item("Master Spring",
bean.addItem(item1);
bean.addItem(item2);
bean.addItem(item3);
bean.removeItem(item2);
result=bean.getTotalPrice();
System.out.println("TotalPrice :"+result);
} catch (RemoteException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (Exception e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
14,右擊項目MySessionEjb,在彈出菜單中選擇”導出”,在導出對話框選擇”J2EE-EAR file”,如圖1-9
單擊下一步,將部署位置設置爲”%Jboss_Home%/server/default/deploy”,若該目錄已存在EAR文件,則應歡迎勾選”Overwrite existing file”,單擊完成.設置如圖1-10
15,在右下角Server視圖,啓動Jboss 4.0,右擊Test.java文件,選擇”運行方式-Java應用程序”.若控制檯出現” TotalPrice :