AccountManager的Home接口 package banking import java rmi RemoteException; import javax. ejb. * public interface AccountManagerHome extends EJBHome { public AccountManager create throws RemoteException, CreateException;
AccountManager的Home接口 package banking; import java.rmi.RemoteException; import javax.ejb.*; public interface AccountManagerHome extends EJBHome { public AccountManager create() throws RemoteException, CreateException; }
AccountManager的 Remote接口 package banking import javax. ejb. import java rmi K public interface AccountManager extends eJBobject public void deposit(String customerName, double amount) throws RemoteEXception public double withdraw(String customer Name double amount throws RemoteEXception, Insufficient FundsException; public double getBalance(string customerName) throws RemoteEXception; public double calculateInterest(String customer Name) throws Remote Exception: public Account createAccount(String customerName, double initialBalance) throws NoAccountCreated Exception, RemoteException; public void removeAccount(string customer Name) throws NoAccountCreated Exception, RemoteException;
AccountManager的Remote接口 package banking; import javax.ejb.*; import java.rmi.*; public interface AccountManager extends EJBObject { public void deposit(String customerName, double amount) throws RemoteException; public double withdraw(String customerName, double amount) throws RemoteException, InsufficientFundsException; public double getBalance(String customerName) throws RemoteException; public double calculateInterest(String customerName) throws RemoteException; public Account createAccount(String customerName, double initialBalance) throws NoAccountCreatedException, RemoteException; public void removeAccount(String customerName) throws NoAccountCreatedException, RemoteException; }
Servlet BankServlet //import 连续调用home接口的 create方法 public class Bankservlet extends Httpservlett remote接口的 getBalance方法、 public void doget(Http SermletrEquest req Http Servletred createaccount方法和 throws ServletException, IOEXception removeAccount方法 resp. setContentType("text/html") PrintWriter out= resp. getWritero out printIn ("<HTML> \n<HEAD><TITLE>GetDemo< Context initial new InitialContext(; AccountManagerHome accountManagerHome=(AcceantManagerHome)PortableRemoteObject narrow(initial lookup ("java: comp/env/ejb /AccountManager"), AccountManagerHome dass); AccountManager account Manager accountManagerHome. create( out println 'name1's balance is: accountManager, getBalance (name 1 ) Account account= accountManager. createAccount( new" 1000.000); out printIn "createAccount successfully<br>); accountManager. removeAccoutn newAccount) out.printlnc'removeAccount successfully<br> catch ( out println (</BODY>); out println ("</HTML> ) out close
Servlet BankServlet //import ... public class BankServlet extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<HTML>\n<HEAD><TITLE>GetDemo</TITLE></HEAD>\n"<BODY>"); try{ Context initial = new InitialContext(); AccountManagerHome accountManagerHome = (AccountManagerHome)PortableRemoteObject. narrow(initial.lookup("java:comp/env/ejb/AccountManager"), AccountManagerHome.class); AccountManager accountManager = accountManagerHome.create(); out.println(“name1’s balance is: “ + accountManager.getBalance(“name1”)); Account account = accountManager.createAccount(“newAccount", 1000.00f); out.println("createAccount successfully<br>"); accountManager.removeAccoutn(“newAccount”); out.println(“removeAccount successfully<br>"); }catch(...) out.println("</BODY>"); out.println("</HTML>"); out.close(); } } 连续调用home接口的create方法, remote接口的getBalance方法、 createAccount方法和 removeAccount方法
打包EJB模块 File Edit Tools Help ♀□Fes les Applica tio ns. NewBankApp Bank BeanJ AR 9口 Applications Genera es JNDI Na mes Relationships P◆ NewBan kApp JAR FIle Name ♀國 BankBeanJAR ejb-jar-ic jar O AccountBea O AccountManager Bean JAR Dis play Name BankBean JAR O LogBean 9鸥 Server a localhost o ntents □ META-INF 打包好的EB模块包含3个Bean AccountManagerBean Accountbear Log Bea L Descriptio Manifest G la Deployment
打包EJB模块 打包好的EJB模块包含3个Bean: AccountManagerBean AccountBean LogBean
打包Web模块 plication Deployment Too: New BankApp 囹eQ的圆国国圖凹 ♀□Fies lles Applica tions. NewBankApp BankYYe b 9口 Applications JNDI Na mes Resource Env, Refs Resource Refs SecurityRoles ♀佥 NewBankAl ♀③ ManWeb General Context Env. Entries EJB Refs Event Listeners File Refs Filter Mapping WAR File Name ariC war O AccountManagerBy WAR Display Name Banke 9 Servers e口WEB-|NF 打包好的Web模块仅包含一个 Servlet BankServlet L Description Advanced Settings
打包Web模块 打包好的Web模块仅包含一个 Servlet: BankServlet