相关运行环境:
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
静儿 阅读(1052)
评论(7) 编辑 收藏