国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

用j2ee實現(xiàn)的一個簡單的會話bean(譯自j2ee tutorial)

2019-11-18 16:15:56
字體:
供稿:網(wǎng)友

  Getting start(為了和tutorial的章節(jié)名字相對,以后所有章節(jié)采用英文名字)

設置:
在你開始運行所給的應用程序事例前,你應當按照步驟下載和配置服務器。
1.下載示例代碼:
本教程的源代碼在j2eetutorial/examples/src/ejb/converter文件夾下,當你下載并解壓縮tutorial bundle的后,會找到這個目錄。
下載地址:http://java.sun.com/j2ee/download.Html#tutoril
2.下載build工具(ant)
為了運行實例,你必須下載并且安裝j2ee sdk和ant。
j2ee sdk下載地址:http://java.sun.com/j2ee/download.html#sdk;
ant下載地址:http://jakarta.apache.org/builds/jakarta-ant/release/v1.3/bin
按照提示解壓縮或者安裝即可。
3.檢查環(huán)境變量
在系統(tǒng)的環(huán)境變量中,設置如下:
JAVA_HOME: j2se sdk安裝的目錄
J2EE_HOME: j2ee sdk安裝的目錄
ANT_HOME: ant安裝的目錄
PATH: 里面必須含有j2se,j2ee,ant下的bin目錄
4.運行j2ee服務器
在命令提示行下輸入j2ee -verbose
-verbose不是必需的,但是有利于debug。
要停止服務,輸入
j2ee -stop
5.運行deploytool
你可以使用2種模式,命令行或者GUI。這里推薦大家使用GUI。
運行了j2ee后,在命令提示行輸入deploytool。

建立j2ee應用程序
這個簡單的應用程序含有3個組件:一個enterPRise bean,一個j2ee客戶,一個web組件。在建造這些組件之前,你必須建立一個j2ee應用名字叫CoverterApp,這個會被存儲在ConverterApp.ear的文件中
1.在deploytool中,選擇FileNewapplication
2.點Browse。
3.選擇目錄j2eetutorial/examples/src/ejb/converter。
4.輸入文件名字ConverterApp.ear。
5.點New Application。
6.點OK。

建立enterprise bean
一個enterprise bean是一個包含了商業(yè)邏輯的服務器組件。在運行時,客戶通過調(diào)用enterprise bean的方法執(zhí)行商業(yè)邏輯。在這個例子中,這個bean是一個無狀態(tài)的會話bean名字是CoverterEJB。源代碼在j2eetutorial/examples/src/ejb/converter Directory。
一個enterprise bean包含3個部分:
1.Remote interface
2.Home interface
3.Enterprise bean class
一個Remote interface定義了客戶可以調(diào)用的方法,方法實現(xiàn)于enterprise bean代碼,這個實例的Remote interface如下:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;

public interface Converter extends EJBObject {
public BigDecimal dollarToYen(BigDecimal dollars)
throws RemoteException;
public BigDecimal yenToEuro(BigDecimal yen)
throws RemoteException;
}
一個home interface允許一個客戶創(chuàng)建,查詢或者刪除一個enterprise bean。ConverterHome interface含有一個創(chuàng)建方法,返回一個remote interface 類型。
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface ConverterHome extends EJBHome {
Converter create() throws RemoteException, CreateException;
}
這個示例的enterprise bean類名字是ConverterBean。這個類實現(xiàn)2個商業(yè)方法,dollarToYen和yenToEuro。
import java.rmi.RemoteException;
import javax.ejb.sessionBean;
import javax.ejb.SessionContext;
import java.math.*;

public class ConverterBean implements SessionBean {

BigDecimal yenRate = new BigDecimal("121.6000");
BigDecimal euroRate = new BigDecimal("0.0077");

public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2,BigDecimal.ROUND_UP);
}

public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2,BigDecimal.ROUND_UP);
}

public ConverterBean() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
現(xiàn)在你可以去編譯這些類
1.在命令提示行進入j2eetutorial/examples目錄
2.輸入以下命令ant converter
ant自動編譯這些文件,編譯好的文件放在j2eetutorial/examples/build/ejb/converter目錄下。
打包enterprise bean
在deploytool下,選擇FileNewEnterprise Bean
1.點Next
2.在Application button選擇Create New JAR File,在下拉菜單選擇ConverterApp,在JAR Display Name輸入ConverterJAR,點Edit,在數(shù)形目錄上輸入j2eetutorial/examples/build/ejb/converter directory,選擇這些類并且點Add:Converter.class, ConverterBean.class, and ConverterHome.class,點OK,點Next。
3.選擇Bean Type為Session,選擇Stateless,Enterprise Bean Class 下拉菜單選擇ConverterBean, Enterprise Bean Name為ConverterEJB。在Remote Home Interface選擇ConverterHome,在Remote Interface選擇Converter,點Next。
4.點Finish。

創(chuàng)建客戶端
一個j2ee應用程序客戶端是用java寫成的。他和服務器端運行在不同的jvm中。
示例程序的客戶端源代碼j2eetutorial/examples/src/ejb/converter/ConverterClient.java。你在運行ant的同時也同時編譯了此客戶端。
一個客戶端需要執(zhí)行這些基本任務:
1 Locating the home interface
2 Creating an enterprise bean instance
3 Invoking a business method
ConverterHome interface定義了life-cycle methods比如creats,在ConverterClient可以執(zhí)行create方法前,他必須定位并且示例化一個ConverterHome類型的對象。
1 創(chuàng)建initial naming context
Context initial = new InitialContext();
上下文是JNDI的一部分。一個上下文是一個名字和對象binding的集合。
2 取得客戶的環(huán)境命名上下文
Context myEnv = (Context)initial.lookup("java:comp/env");
3 取得binding在ejb/SimpleConverter上的對象
Object objref = myEnv.lookup("ejb/SimpleConverter");
ejb/SimpleConverter被binding在一個enterprise bean引用上,這是一個enterprise bean邏輯的名字。
4 縮小指向ConverterHome對象的引用
ConverterHome home =
(ConverterHome) PortableRemoteObject.narrow(objref,
ConverterHome.class);
為了取得一個bean實例,客戶在ConverterHome調(diào)用create方法
Converter currencyConverter = home.create();
調(diào)用商業(yè)方法也很簡單,這個客戶調(diào)用dollarToYen方法如下:
BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyConverter.dollarToYen(param);
客戶端程序:
The full source code for the ConverterClient program follows.

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.math.BigDecimal;

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();

BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount =
currencyConverter.dollarToYen(param);
System.out.println(amount);
amount = currencyConverter.yenToEuro(param);
System.out.println(amount);

System.exit(0);

} catch (Exception ex) {
System.err.println("Caught an uneXPected exception!");
ex.printStackTrace();
}
}
}
打包客戶端
在deploytool選擇New Application Client
1 點next
2 選擇ConverterApp,點edit,在j2eetutorial/examples/build/ejb/converter目錄下選擇ConverterClient.class點add,點ok,點next。
3 在Main Class下拉菜單選擇,選擇ConverterClient,Display Name為ConverterClient,在Callback Handler Class選擇container-managed authentication,點next,點finish。
4 在主程序的樹選擇ConverterClient,選擇EJB Refs卡片,在Coded Name column輸入ejb/SimpleConverter,在Type column 選擇Session,在Interfaces column選擇Remote,在Home Interface選擇ConverterHome,在Local/Remote Interface column輸入Converter。

建立web客戶端
web客戶端包含在jsp文件中:j2eetutorial/examples/src/ejb/converter/index.jsp。
jsp文件不需要編譯。
打包web客戶端
在deploytool的主界面下選擇FileNewWeb Component
1 next
2 選擇Create New WAR File,并選擇ConverterApp,點edit,在樹形目錄上面輸入j2eetutorial/examples/build/ejb/converter,選擇index.jsp然后點add,點ok,點next。
3 選擇jsp
4 在JSP Filename選擇index.jsp,點finish
5 在主程序的樹選擇ConverterWAR,選擇EJB Refs卡片,點add,在Coded Name column, 輸入ejb/TheConverter,其他和客戶端設置相同。

指定JNDI名稱:
盡管客戶端和web客戶端使用相同的bean,但是他們使用不同的名字來使用這個相同的bean,客戶端使用ejb/SimpleConverter,而web客戶端使用ejb/TheConverter。你必須設置JNDI的名稱。
在樹形目錄下選擇ConverterApp,選擇JNDI Names卡片,在所有的JNDI欄輸入MyConverter。

配置這個J2EE應用程序
1 選擇Tools-〉Deploy
2 選擇Return Client Jar
3 輸入需要生成jar文件的地址
4 點next
5 確認JDNI name正確后點next
6 在WAR Context Root dialog box,輸入converter
7 點next
8 finish

運行J2EE客戶端
1 在命令行進入j2eetutorial/examples/src/ejb/converter目錄
2 保證這個目錄含有ConverterApp.ear
3 設置環(huán)境變量APPCPATH包含ConverterAppClient.jar
4 輸入以下命令 runclient -client ConverterApp.ear -name ConverterClient
-textauth
5 用戶名:guest 密碼:guest123
6 運行成功顯示如下: Binding name:'java:comp/env/ejb/SimpleConverter'
12160.00
0.77
Unbinding name:'java:comp/env/ejb/SimpleConverter'

運行web客戶端
在瀏覽器地址輸入http://:8000/converter
即可
host是本機名或者localhost

(出處:http://m.survivalescaperooms.com)



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长垣县| 荥经县| 泰顺县| 吴忠市| 莱阳市| 墨竹工卡县| 金阳县| 托克逊县| 常德市| 龙胜| 临泽县| 乌鲁木齐市| 呼伦贝尔市| 铅山县| 南江县| 伊春市| 西贡区| 青龙| 汝州市| 台东市| 抚顺县| 资中县| 嘉定区| 安远县| 锦屏县| 长寿区| 镇巴县| 芦山县| 儋州市| 铜鼓县| 尖扎县| 图们市| 沙湾县| 揭阳市| 微山县| 措勤县| 榆树市| 准格尔旗| 武穴市| 日喀则市| 岐山县|