服务热线:13616026886

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

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

windows2000下安装j2ee和部署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
  显示以下信息表示运行成功:(不同的版本显示可能不同)
  j2ee server listen port: 1050
  naming service started:1050
  binding datasource, name = jdbc/estoredb, url = jdbc:cloudscape:rmi:cloudscapedb;create=true
  binding datasource, name = jdbc/db2, url = jdbc:cloudscape:rmi:cloudscapedb;create=true
  binding datasource, name = jdbc/cloudscape, url = jdbc:cloudscape:rmi:cloudscapedb;create=true
  binding datasource, name = jdbc/inventorydb, url = jdbc:cloudscape:rmi:cloudscapedb;create=true
  binding datasource, name = jdbc/db1, url = jdbc:cloudscape:rmi:cloudscapedb;create=true
  binding datasource, name = jdbc/xacloudscape, url = jdbc/xacloudscape__xa
  binding datasource, name = jdbc/xacloudscape__xa, datasource = com.cloudscape.core.remotexadatasource@330913
  starting jms service ... initialization complete - waiting for client requests
  binding : < jms destination : jms/queue , javax.jms.queue >
  binding : < jms destination : jms/topic , javax.jms.topic >
  binding : < jms cnx factory : jms/topicconnectionfactory , topic , no properties >
  binding : < jms cnx factory : topicconnectionfactory , topic , no properties >
  binding : < jms cnx factory : jms/queueconnectionfactory , queue , no properties >
  binding : < jms cnx factory : queueconnectionfactory , queue , no properties >
  starting web service at port:8000
  starting secure web service at port:7000
  apache tomcat/4.0-b4-dev
  starting web service at port:9191
  apache tomcat/4.0-b4-dev
  j2ee server startup complete.
  
  启动成功后,在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的入门,简单而明了。本程序由charly设计编写,并由其本人多次运行确保无误。如不能正常运行,请检查你的环境变量设置是否正确,最好不要直接将以上各命令copy到dos窗口中运行,某些字符可能受中文全角字符的影响而不被识别。另外如果某个应用程序部署不成功,可以

扫描关注微信公众号