相關運行環境:
1.jdk150_04
2.eclipse3.2
3.weblogic server9.2
4.wtp-R-1.5.4-200705021353(里面包含ejb插件)
5.xdoclet-1.2.3
ejb工程的創建:
基本上直接點擊“下一步”即可。但要注意看
建完的工程是否含有以下libraries:
1.jre system libraries
2.generic bea weblogic server v9.2
3.weblogic.jar
4.ear libraries
這樣系統會自動生成配置文件。
實例:
實現功能:
從服務器端取系統時間,與客戶端時間求時間間隔。
服務器端代碼:
bean里的foo()(其他按照自動生成的即可)
public Calendar foo(String param) {
Calendar calCurrent = Calendar.getInstance();
Date timeCurrent=new Date();
calCurrent.setTime(timeCurrent);
return calCurrent;
}
客戶端代碼:
創建一個新類:
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.NamingException;
public class TestClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "t3://localhost:7001";
// Hashtable env = new Hashtable();
// env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
// env.put(Context.PROVIDER_URL, url);
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
int dayCurrent = 0;
Test my = null;
try {
my = TestUtil.getHome(properties).create();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (CreateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NamingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
dayCurrent = my.foo("").get(Calendar.DAY_OF_YEAR);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar birthday = Calendar.getInstance();
int year = 0;
int month = 0;
int day = 0;
System.out.println("誕生日を入力してください:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("年:");
year = Integer.parseInt(in.readLine());
System.in.skip(6);
System.out.print("月:");
month = Integer.parseInt(in.readLine())-1;
System.in.skip(6);
System.out.print("日:");
day = Integer.parseInt(in.readLine());
} catch (IOException e) {
System.out.print("フォーマットが違います。");
}
birthday.set(year, month, day);
int yearCount=0;
int dayBirthday = birthday.get(Calendar.DAY_OF_YEAR);
int dayCount = dayBirthday - dayCurrent;
try {
yearCount=my.foo("").get(Calendar.YEAR)-birthday.get(Calendar.YEAR);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (dayCount >= 0)
System.out.println("あなたの"+yearCount+"才誕生日は" + dayCount + "日後です。");
else
System.out.println("あなたの"+yearCount+"才誕生日は" + Math.abs(dayCount) + "日前です。");
}
}
運行結果:
誕生日を入力してください:
年:1986
月:10
日:12
あなたの21才誕生日は24日前です。
posted on 2007-11-05 11:26
靜兒 閱讀(1059)
評論(7) 編輯 收藏