下圖是這個(gè)文件的源代碼。MyejbMd package myejb.maxuan.ejb.model;
import java.io.*;
public class MyejbMd implements Serializable {
public MyejbMd() { } public String dlDm; public String dlMc; public String action; public String getDlDm() { return dlDm; } public void setDlDm(String dlDm) { this.dlDm = dlDm; } public String getDlMc() { return dlMc; } public void setDlMc(String dlMc) { this.dlMc = dlMc; } public String getAction() { return action; } public void setAction(String action) { this.action = action; } private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); } }
然后我們需要編輯實(shí)體BEAN文件來(lái)引入模型和對(duì)數(shù)據(jù)庫(kù)的基本操作。 Myejb.java文件 package myejb.maxuan.ejb;
import javax.ejb.*; import java.util.*; import myejb.maxuan.ejb.model.*;
public interface Myejb extends javax.ejb.EJBLocalObject { public String getDlDm(); public void setDlMc(String dlMc); public String getDlMc(); public MyejbMd getDlMd(); public void setMyejbMd(MyejbMd dlMd); }
MyejbBean.java文件 package myejb.maxuan.ejb;
import javax.ejb.*; import myejb.maxuan.ejb.model.*;
abstract public class MyejbBean implements EntityBean { EntityContext entityContext; public java.lang.String ejbCreate(java.lang.String dlDm) throws CreateException { setDlDm(dlDm); return null; } public void ejbPostCreate(java.lang.String dlDm) throws CreateException { /**@todo Complete this method*/ } public java.lang.String ejbCreate(java.lang.String dlDm, java.lang.String dlMc) throws CreateException { setDlDm(dlDm); setDlMc(dlMc); return null; } public void ejbPostCreate(java.lang.String dlDm, java.lang.String dlMc) throws CreateException { /**@todo Complete this method*/ } public void ejbRemove() throws RemoveException { /**@todo Complete this method*/ } public abstract void setDlDm(java.lang.String dlDm); public abstract void setDlMc(java.lang.String dlMc); public abstract java.lang.String getDlDm(); public abstract java.lang.String getDlMc(); public void ejbLoad() { /**@todo Complete this method*/ } public void ejbStore() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void unsetEntityContext() { this.entityContext = null; } public void setEntityContext(EntityContext entityContext) { this.entityContext = entityContext; } public myejb.maxuan.ejb.model.MyejbMd getDlMd() { MyejbMd dlMd = new MyejbMd(); dlMd.setDlDm(getDlDm()); dlMd.setDlMc(getDlMc()); return dlMd; } public void setMyejbMd(MyejbMd dlMd){ setDlMc(dlMd.getDlMc()); } }
接著我們要編輯最后的MyejbHome.java,雙擊左邊樹(shù)的EJB,在右邊的面板中選中Myejb后點(diǎn)右鍵,在出現(xiàn)的菜單中選ADD,選取下面的Findel方法,如下圖所示:
在接著出現(xiàn)的對(duì)話(huà)框中選擇填寫(xiě)如下:
我們繼續(xù)編輯MyejbHome.java文件 package myejb.maxuan.ejb; import javax.ejb.*; import java.util.*;
public interface MyejbHome extends javax.ejb.EJBLocalHome { public Myejb create(String dlDm) throws CreateException; public Myejb create(String dlDm, String dlMc) throws CreateException; public Collection findAll() throws FinderException; public Myejb findByPrimaryKey(String dlDm) throws FinderException; }
好了,實(shí)體BEAN文件我們已經(jīng)寫(xiě)完了,現(xiàn)在我們編譯一下,看有沒(méi)有什么錯(cuò)誤。 新手提示:在引入模型之前,我們需要先編譯一下模型文件。 在接下來(lái)我們將寫(xiě)一個(gè)SessionBean。
我們繼續(xù)來(lái)完成這個(gè)EJB,我們需要來(lái)寫(xiě)一個(gè)SessionBean,創(chuàng)建方法與創(chuàng)建實(shí)體BEAN一樣,這里不在多說(shuō),只把它們的源碼貼出,請(qǐng)大家仔細(xì)看,理解其中的意義! 這里我將SessionBean命名為MyejbWeb MyejbWeb.java package myejb.maxuan.ejb;
import javax.ejb.*; import java.util.*; import java.rmi.*; import java.sql.*; import java.io.*; import myejb.maxuan.ejb.model.*;
public interface MyejbWeb extends javax.ejb.EJBObject { public ArrayList getDlList() throws RemoteException; public MyejbMd getDl(String dlDm) throws RemoteException; }
MyejbWebBean.java package myejb.maxuan.ejb;
import javax.ejb.*; import java.util.*; import java.sql.*; import java.sql.Date; import java.io.InputStream; import java.io.OutputStream; import myejb.maxuan.ejb.model.*;
public class MyejbWebBean implements SessionBean { SessionContext sessionContext; public void ejbCreate() throws CreateException { /**@todo Complete this method*/ } public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public ArrayList getDlList(){ ArrayList dlList=new ArrayList(); try { javax.naming.Context context = new javax.naming.InitialContext(); MyejbHome dlHome= (MyejbHome)context.lookup("Myejb"); Collection dlCntl = dlHome.findAll(); Iterator iterator = dlCntl.iterator(); while(iterator.hasNext()){ Myejb myEjb = (Myejb) iterator.next(); MyejbMd dlMd = myEjb.getDlMd(); dlList.add(dlMd); } }catch (Exception ex) { throw new EJBException(ex); } return dlList; } public MyejbMd getDl(String dlDm){ MyejbMd dlMd=new MyejbMd(); try { javax.naming.Context context = new javax.naming.InitialContext(); MyejbHome dlHome= (MyejbHome)context.lookup("Myejb"); try{ Myejb myEjb = dlHome.findByPrimaryKey(dlDm); dlMd = myEjb.getDlMd(); }catch(FinderException fe){ dlMd=null; } }catch (Exception ex) { throw new EJBException(ex); } return dlMd; } }
MyejbWebHome.java package myejb.maxuan.ejb;
import javax.ejb.*; import java.util.*; import java.rmi.*;
public interface MyejbWebHome extends javax.ejb.EJBHome { public MyejbWeb create() throws CreateException, RemoteException; }
至此,我們的EJB部份已經(jīng)完成,明天我們將繼續(xù)寫(xiě)一個(gè)javabean來(lái)訪問(wèn)這個(gè)EJB!同時(shí)我們也會(huì)引入struts,請(qǐng)大家留意!
PS:雖然這只是一個(gè)簡(jiǎn)單的EJB網(wǎng)絡(luò)應(yīng)用結(jié)構(gòu),但卻是建立J2EE大型應(yīng)用的基礎(chǔ),希望大家多多交流!
本文鏈接:http://m.95time.cn/tech/program/2004/1603.asp
出處:藍(lán)色理想
責(zé)任編輯:藍(lán)色
上一頁(yè) [J2EE] 實(shí)戰(zhàn)開(kāi)發(fā)EJB [1] 下一頁(yè)
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|