新聞中心
本篇內(nèi)容介紹了“怎么創(chuàng)建J2EE應用程序客戶端”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
益陽網(wǎng)站建設公司創(chuàng)新互聯(lián),益陽網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為益陽成百上千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的益陽做網(wǎng)站的公司定做!
J2EE應用程序客戶端由JavaTM語言編寫,在運行的時候,客戶端程序和J2EE SERVER執(zhí)行在不同的虛擬機(VM)中
在這個例子中,J2EE應用程序客戶端需要兩個不同的JAR文件.第一個JAR文件是客戶端的J2EE組件.這個JAR文件包含客戶端的deployment descriptor和它的類文件.當你運行New Application Client wizard,
deploytool
自動創(chuàng)建JAR文件然后把它存到應用程序的EAR文件中. 由J2EE規(guī)范定義的JAR文件可以方便的跨越所有兼容J2EE的服務器.第二個JAR文件包含客戶端程序運行時必需的stub類.這些stub類使客戶端可以訪問運行在J2EE server上的enterprise beans. Because this second JAR file is not covered by the J2EE Specifications, it is implementation-specific, intended only for the J2EE SDK.
J2EE應用程序客戶端源代碼在
examples/src/EJB/converter/ConverterClient.java
中.在這個章節(jié)中,你已經(jīng)把它隨同enterprise bean代碼一起編譯, 編譯源碼文件.編寫J2EE應用程序客戶端
ConverterClient.java
源代碼說明了enterprise bean的客戶端執(zhí)行的基本任務:
定位home interface
創(chuàng)建enterprise bean實例
調(diào)用商務方法
定位Home Interface
ConverterHome
接口定義象create
一樣具有生命周期的方法.在ConverterClient
能調(diào)用create
方法之前,它必須例示(instantiate)ConverterHome
類型的對象. 它分為3步處理:
創(chuàng)建JNDI naming context.
Context initial = new InitialContext();取得綁定到
ejb/SimpleConverter
的對象.object objref = initial.lookup ("java:comp/env/ejb/SimpleConverter");Narrow the reference to a
ConverterHome
object.ConverterHome home = (ConverterHome) PortableRemoteObject.narrow(objref, ConverterHome.class);創(chuàng)建一個Enterprise Bean實例
要創(chuàng)建bean實例,客戶端調(diào)用
ConverterHome
中的create
方法并且返回一個Converter
類型的對象.遠程Converter
接口定義客戶端可以調(diào)用的在bean中的商務方法.當客戶端調(diào)用create
方法時,EJB容器例示(instantiates)bean然后調(diào)用ConverterBean.ejbCreate
方法. 客戶端調(diào)用create
方法如下所示:Converter currencyConverter = home.create();調(diào)用商務方法
調(diào)用一個商務方法非常容易--你只是調(diào)用
Converter
對象上的方法。EJB容器將在運行于服務端的ConverterEJB
實例上調(diào)用相應的方法. 下面一行代碼是客戶端調(diào)用dollarToYen
上的商務方法.double amount = currencyConverter.dollarToYen(100.00);ConverterClient源代碼
完整的
ConverterClient
源碼如下.import javax.naming.Context; import javax.naming.InitialContext; import javax.Rmi.PortableRemoteObject; import Converter; import ConverterHome; public class ConverterClient { public static void main(String[] args) { try { Context initial = new InitialContext(); Object objref = initial.lookup ("java:comp/env/ejb/SimpleConverter"); ConverterHome home = (ConverterHome)PortableRemoteObject.narrow( objref, ConverterHome.class); Converter currencyConverter = home.create(); double amount = currencyConverter.dollarToYen(100.00); System.out.println(String.valueOf(amount)); amount = currencyConverter.yenToEuro(100.00); System.out.println(String.valueOf(amount)); currencyConverter.remove(); } catch (Exception ex) { System.err.println("Caught an unexpected exception!"); ex.printStackTrace(); } } }編譯應用程序客戶端
應用程序客戶端文件和enterprise bean文件同時被編譯,可參考編譯源文件中所述.
打包J2EE應用程序客戶端
打包應用程序客戶端組件, 你需要運行
deploytool
的New Application Client Wizard. 在這個過程中, 向?qū)О芽蛻舳宋募幾g成一個JAR文件然后把這個JAR文件加到應用程序的ConverterApp.ear
文件中.啟動New Application Client Wizard,選擇File->New Application Client. 向?qū)э@示下面的對話框.
Introduction對話框:
閱讀關(guān)于向?qū)匦愿庞[的說明.
單擊 Next.
JAR File Contents對話框
在這個組合框中,選擇 ConverterApp.
單擊 Edit.
在Available Files目錄樹中, 定位到
examples/build/ejb/converter
目錄.選擇ConverterClient.class file文件然后單擊Add.
單擊 OK.
單擊 Next.
常規(guī)對話框:
在Main Class組合框,選擇 ConverterClient.
檢驗Display Name欄的內(nèi)容是ConverterClient.
在Callback Handler Class組合框,選擇 container-managed authentication.
單擊 Next.
單擊 Finish.
指定應用程序客戶端的Enterprise Bean Reference
當調(diào)用
lookup
方法時,ConverterClient引用一個enterprise bean:Object objref = initial.lookup ("java:comp/env/ejb/SimpleConverter");如下指定reference:
在樹目錄中,選擇ConverterClient.
選擇EJB Ref's tab.
單擊 Add.
在Coded Name列輸入
ejb/SimpleConverter
.在Type列,選擇 Session.
在Interfaces列,選擇Remote.
在Home列輸入
ConverterHome
.在Remote列輸入
Converter
.
“怎么創(chuàng)建J2EE應用程序客戶端”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)頁名稱:怎么創(chuàng)建J2EE應用程序客戶端
當前地址:http://fisionsoft.com.cn/article/jgeijj.html