溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么創(chuàng)建J2EE應用程序客戶端

發(fā)布時間:2022-01-07 19:59:14 來源:億速云 閱讀:117 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“怎么創(chuàng)建J2EE應用程序客戶端”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

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步處理:

  1. 創(chuàng)建JNDI naming context.

    Context initial = new InitialContext();


  2. 取得綁定到ejb/SimpleConverter的對象.

    object objref = initial.lookup ("java:comp/env/ejb/SimpleConverter");


  3. 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ū)э@示下面的對話框.

  1. Introduction對話框:

    1. 閱讀關(guān)于向?qū)匦愿庞[的說明.

    2. 單擊 Next.

  2. JAR File Contents對話框

    1. 在這個組合框中,選擇 ConverterApp.

    2. 單擊 Edit.

    3. 在Available Files目錄樹中, 定位到examples/build/ejb/converter目錄.

    4. 選擇ConverterClient.class file文件然后單擊Add.

    5. 單擊 OK.

    6. 單擊 Next.

  3. 常規(guī)對話框:

    1. 在Main Class組合框,選擇 ConverterClient.

    2. 檢驗Display Name欄的內(nèi)容是ConverterClient.

    3. 在Callback Handler Class組合框,選擇 container-managed authentication.

    4. 單擊 Next.

    5. 單擊 Finish.

指定應用程序客戶端的Enterprise Bean Reference

當調(diào)用lookup方法時,ConverterClient引用一個enterprise bean:

Object objref = initial.lookup ("java:comp/env/ejb/SimpleConverter");


如下指定reference:

  1. 在樹目錄中,選擇ConverterClient.

  2. 選擇EJB Ref's tab.

  3. 單擊 Add.

  4. 在Coded Name列輸入ejb/SimpleConverter.

  5. 在Type列,選擇 Session.

  6. 在Interfaces列,選擇Remote.

  7. 在Home列輸入ConverterHome.

  8. 在Remote列輸入Converter.

“怎么創(chuàng)建J2EE應用程序客戶端”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI