服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

从零开始开发一个分布式的j2ee应用程序

1. 安装

可以从以下网址下载一个j2ee(j2sdkee-1_3-beta2-win.exe):http://java.sun.com/j2ee/j2sdkee-beta/index.html"。也许你已装了旧版的j2ee sdk 产品,如果是,在安装新下载的j2ee之前请先卸载或删掉旧版的j2ee sdk。运行j2sdkee-1_3-beta2-win.exe,按安装步骤安装好j2ee。这里假设你的j2ee安装在:c:\j2sdkee1.3 目录下。

2. 设置环境变量

在运行j2ee sdk之前,你必须设置以下环境变量:

j2ee_home - 你的j2ee sdk所安装的目录。如本例中的:c:\j2sdkee1.3 。

java_home - 你的java 2 sdk 所安装的目录。

path - 设置为你安装j2ee sdk目录下的bin目录。如本例的的:c:\j2sdkee1.3\bin 。

classpath - 增添%j2ee_home%\lib\j2ee.jar到classpath中。本例中也可写为:c:\j2sdkee1.3\lib\j2ee.jar

3. 运行j2ee

dos命令行敲入以下命令:

%j2ee_home%\bin\j2ee -verbose

启动成功后,在ie浏览器中访问http://localhost:8000 可以看到默认的主页信息。

4. 编写和运行helloworld程序

j2ee应用程序一般使用rmi(远程方法调用)来完成客户端与服务器的交互。当然,其间也少不了ejb的作用。本例为一个j2ee应用程序:客户端向服务器发送一个问候语:“hello,remote object”。服务器收到该问候语后打印该问候语,并返回一字符串作为应答。客户端收到此应答后打印它。

remoteinterface.java

/**
* 第一步:
* 定义一个新的接口继承javax.ejb.ejbobject。
* 新定义的接口中的每一个方法都必须抛出
* java.rmi.remoteexception异常。
*/
public interface remoteinterface extends javax.ejb.ejbobject
{
public string message(string str)throws java.rmi.remoteexception;
}

remoteobject.java

/**
* 第二步:
* 定义一个类来实现javax.ejb.sessionbean接口。
* 并在该类中实现在第一步中编写的接口中所定义的方法。
*/
public class remoteobject implements javax.ejb.sessionbean
{
public string message(string str)throws java.rmi.remoteexception
{
system.out.println("remote object received from client:
  \""+str+"\""); //打印(从客户端)接收到的字符串。
return "hello,i'm remote object,i received your message:
  \'"+str+"\'"; //返回一应答字符串。
}

public remoteobject() {}
public void ejbcreate() {}
public void ejbremove() {}
public void ejbactivate() {}
public void ejbpassivate() {}
public void setsessioncontext(javax.ejb.sessioncontext sc) {}
}

remotehome.java

/**
* 第三步:
* 定义一个类继承javax.ejb.ejbhome 。
*/
public interface remotehome extends javax.ejb.ejbhome
{
remoteinterface create()throws java.rmi.remoteexception,
 javax.ejb.createexception;
}

client.java

/**
* 第四步:
* 定义客户端类。
*/
public class client
{
public static void main(string[] args)
{
try
{
javax.naming.context initcontext=new javax.naming.initialcontext();
object obj=initcontext.lookup("helloworld"); 
//远程查找,由名字得到对应的对象。
remotehome home=(remotehome)javax.rmi.portableremoteobject.
 narrow(obj,remotehome.class);
remoteinterface remote=home.create();
string receivefromremote=remote.message("hello,remote object!");
//远程方法调用
system.out.println("client received from remote object: 
 \""+receivefromremote+"\"");
}
catch(exception e)
{
e.printstacktrace();
}
}
}

假设以上四个java文件存于c:\helloworld\下,编译它们如:c:\helloworld>javac *.java 。

5. 部署应用程序

启动application dopolyment tool:新开一个dos窗口,键入以下命令,%j2ee_home%\bin\deploytool 。该工具启动速度可能比较慢,要耐心等待。启动成功后会出现主界面(此时不要关闭dos窗口)。在该界面中选 择 file菜 单 ,再选new application项。在 application file name 输 入 :c:\helloworld\helloworld.ear 。在 application disply name 输 入 你所喜欢的显示名如:helloworld。点 击 ok,在主界面的树形结构files-->applications下将增加新的一项:helloworld。这意味着产生了一个新的应用程序。接下来我们要做的就是部署该应用程序。在主界面的树形结构下选中helloworld,然后再在主界面的file菜单中选取new-->enterprise bean,在弹出的名为“new enterprise bean - introduction”窗口中选取next跳过第一步,在接下来的一步中,create new ejb file in application项中选helloworld,在ejb display name中填上你喜欢的名字如:hello world ejb,点击edit按钮,在弹出的窗口中,start directory中填:c:\helloworld\,在available files中展开树形结构c:\helloworld\,选取remoteinterface.class、remoteobject.class、remotehome.class、client.class四项,点add按钮添加,然后按ok确定。此时在contents框中增加了该四个class。点next进入下一步。session项选stateless,意为不保存session状态。enterprise bean class选remoteobject。enterprise bean name中填上你喜欢的名字如:hello world bean。remote home interface中选remotehome,remote interface中选remoteinterface。选next进入下一步。接下来的步骤可直接点finish。这时主界面的树形结构中files-->application-->hello world中将出现hello world ejb-->hello world bean子项。在主界面的树形结构下选中hello world,然后再在主界面的tools菜单中选取deploy,将弹出新的窗口名为“deploy hello world - introduction”。object to deploy中选hello world,target server中选localhost,选中retuen client jar,在client jar file name中填上:c:\helloworld\helloworldclient.jar。选next进入下一步,在application框的jndi name框中双击并填上helloworld,注意必须与client.java中object obj=initcontext.lookup("helloworld")的“helloworld”保持一致。点next进入下一步。点finish完成。这时将出现deployment progress窗口。如果有误,该窗口将出现异常信息。如果一切正常,点ok便完成了部署工作。

6. 运行应用程序

新开一个dos窗口。进入c:\helloworld\classes目录下运行:c:\ helloworld\classes>java -classpath %j2ee_home%\lib\j2ee.jar;.;helloworldclient.jar; client 。运行成功则出现如下信息:client received from remote object: "hello,i'm remote object,i received your message: 'hello,remote object!'" 。而服务端dos窗口(j2ee -verbose)中出现如下信息:remote object received from client: "hello,remote object!" 。

好,到了这里大家应该都开始了一个真正的j2ee程序了吧?

可是现在有一个问题,ejb和j2ee的优点是在哪儿?什么是分布式的,在一台机器上那还叫什么分布系统吗?下面我将描述怎么把上面的程序变成分布式的。

如果您直接把client.class copy 到其他的机器上,肯定执行不了。因为你还需要一个home和remote接口,这两个文件在什么地方呢?在helloclient.jar这个包里面!所以你还需要这个包。把那个包也copy过去,执行一下,还是不可以。因为你的jndi名,只是个名字,程序怎么知道你要连的服务器在哪儿呀?所以原来的client代码你需要做如下的改动。改成:

properties env = new properties();

env.put(javax.naming.context.initial_context_factory,

"com.sun.jndi.cosnaming.cnctxfactory");

env.put(javax.naming.context.provider_url,"iiop://wcy:1050");

//javax.naming.context initcontext=

new javax.naming.initialcontext(env);

context initcontext = new initialcontext(env);

object obj=initcontext.lookup("helloworld");

//远程查找,由名字得到对应的对象。

remotehome home=(remotehome)javax.rmi.portableremoteobject.

narrow(obj,remotehome.class);

remoteinterface remote=home.create();

string receivefromremote=remote.message("hello,remote object!");

//远程方法调用

system.out.println("client received from remote object:

\""+receivefromremote+"\"");

重新编译javac *.java成功后。把这个新的类copy到其他的机器上。

打入java -classpath %j2ee_home%\lib\j2ee.jar;.;helloworldclient.jar; client

扫描关注微信公众号