服务热线:13616026886

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

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

win2000下jboss开发环境配置


  说明: 此节针对ejb开发初学者,主要是手动配置一个最基本的基于文本开发的环境,让初学者从头开始编写各类程序和配置文件,以更清楚的了解 ejb 开发和部署的步聚与原理。这是作者在学习 ejb 开发时的一个总结,欢迎指正。

  一、需求软件列表及下载地址

  1、 sun java sdk ,我们采用的版本为: j2sdk1.4.1_01 ,下载地址为: http://www.sun.com/download/ ,下载后的文件名为: j2sdk-1_4_1_01-windows-i586.exe

  2、 jboss ,一个免费的 ejb 容器。我们采用版本为: jboss-3.2.1_tomcat-4.1.24 ,这是一个内部己集成 tomcat 的 ejb 容器。下载地址为: http://www.jboss.org/ ,下载后的文件名为: jboss-3.2.1_tomcat-4.1.24.zip 。

  3、 注意:作者只保证在以上软件版本下以下步聚成功,对于其它的版本也应相同,但作者没有求证。以下步聚请严格按照执行。

  二、安装所需软件

  1、 安装 sdk 到 c:/j2sdk1.4.1_01 ;方法为:双击执行下载的 j2sdk-1_4_1_01-windows-i586.exe 文件,选择安装目录为 c:/j2sdk1.4.1_01 ,其它默认即可。

  2、 安装 jboss ;方法为:将下载的 jboss-3.2.1_tomcat-4.1.24.zip 文件解压到任意一个目录,解压后会在此目录中产生一个 jboss-3.2.1_tomcat-4.1.24 文件夹,在此文件夹下有一系列的文件和文件夹。然后在 c 盘根目录下建一个 jboss 目录,并将刚才解压后 jboss-3.2.1_tomcat-4.1.24 文件夹下的一系列文件和文件夹全都拷贝到 c : /jboss 目录下。即将 jboss 安装到了 c:/jboss 目录中。

  三、配置运行环境及测试程序目录

  一、设置环境变量

  1、 设置 java_home 系统变量:在 win2k 下方法为,在桌面上右键点击“我的电脑”,选择属性,然后在弹出的页面上点“高级” -> “环境变量”,然后在弹出的页面中“系统变量”下点击“新建”,然后在弹出的窗口中“变量名 (n) 输入“ java_home ”,在“变量值 (v) ”一栏中输入“ c:/j2sdk1.4.1_01 ”。

  2、 以同样的方法设置: jboss_home 值为: c:/jboss

  3、 以同样的方法在上面地方将 path 的值后追加:以下内容:“ c:/j2sdk1.4.1_01/bin;c:/jboss/bin ”

  二、创建程序目录

  1、 在 c:/jboss 下新建一个 myproject 目录,用于存放自己的所有项目。

  2、 在 myproject 目录下新建三个目录:一个是 jsp 目录,其下用于存放所有的 jsp 文件和 servlet 文件及其它的 html , image 文件等;一个是 ejb 目录,用于存放所有即将发布的 ejb 类文件;一个是 src 目录,用于存放所有的 java 源文件。

  3、 在 jsp 目录下新建一个目录,并命名为: hello.war ,用来发布 web 程序,然后再在此目录下新建一系列文件夹:即在 hello.war 下新建 web-inf (大写)文件夹,再在 web-inf 下新建 classes 文件夹,再在 classes 文件夹下新建一个 hello 文件夹。

  4、 在 ejb 目录下新建一个 client 目录和一个 hello.jar 目录,分别用于存放 ejb 客户端和要发布的服务器端类。然后再在 client 目录下新建一个 hello 目录,再在此目录下新建一个 ejb 目录。然后在 hello.jar 目录下新建一个 hello 目录和一个 meta-inf( 大写 ) 目录,并在 hello 目录中新建一个 ejb 目录。


  四、编写测试程序及相关脚本

  一、 测试 jboss 安装成功与否,方法如下:进入 c : /jboss 目录中,执行 run.bat ?cc all 启动具有全部功能的 jboss 配置服务器。屏幕经过一会滚动后不再滚动。这时启动 ie 输入: http://localhost:8080/jmx-console/index.jsp 即可看到 ie 中出现一系列列表,即表明 jboss 启动成功。此时可在启动 jboss 的 dos 窗口上按 ctrl+c 关闭 jboss 服务器。

  二、 进入 c:/jboss/myproject/jsp/hello.war 目录中,用记事本新建一个 index.jsp 文件,内容如下:用于测试 jsp 程序

<%@page contenttype="text/html; charset=gb2312"%>
<html>
<head>
 <title></title>
 <meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#ffffff" text="#000000" topmargin="5">
 <%
  string hello = "hello world!this created by jsp file!";
  out.println(hello);
 %>
</body>
</html>

  三、 进入 c:/jboss/myproject/src 目录,新建一个 helloworld.java ,是一个 servlet 类,内容如下:

package hello;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class helloworld extends httpservlet
{
 public void doget(httpservletrequest request, httpservletresponse response) throws ioexception, servletexception
 {
  response.setcontenttype("text/html");
  printwriter out = response.getwriter();
  out.println("<hr>");
  out.println("hello world! this is created by servlet!");
  out.println("<hr>");
 }
}

  到此,我们就建立好了用于测试的 jsp 文件和 servlet 类,下面将建 helloworld 的无状态 sessionbean 。

  四、进入 c:/jboss/myproject/src 目录,新建一个 hello.java 文件,以实现 ejb 的 remote 接口,内容如下,

package hello.ejb;
/**
 * remote interface for enterprise bean: hello
 * remote 接口
*/

public interface hello extends javax.ejb.ejbobject {

/**
 * 唯一的方法 hello ,向客户端返回问候。
*/
public string hello() throws java.rmi.remoteexception;
}

  同样在此目录中新建一个 hellolocal.java 文件,实现 local 接口,内容如下:

package hello.ejb;
import javax.ejb.ejblocalobject;
public interface hellolocal extends ejblocalobject {
 public string hello();
}

  同样在此目录中新建一个 hellohome.java 文件,实现 home 接口,内容如下:

package hello.ejb;

/**
 * home interface for enterprise bean: hello
 * home 接口
*/

public interface hellohome extends javax.ejb.ejbhome{

/**
 * creates a default instance of session bean: hello
*/
public hello create() throws javax.ejb.createexception, java.rmi.remoteexception;

}

  同样在此目录中新建一个 hellolocalhome.java 文件,实现 localhome 接口,内容如下:

package hello.ejb;
import javax.ejb.ejblocalhome;

public interface hellolocalhome extends ejblocalhome {
 hellolocal create() throws javax.ejb.createexception;
}

  同样在此目录中新建一个 hellobean.java ,实现 ejb 类,内 [ 容如下:

 
package hello.ejb;

/**
 * bean implementation class for enterprise bean: hello
*/

public class hellobean implements javax.ejb.sessionbean{
 private javax.ejb.sessioncontext mysessionctx;
 /**
  * getsessioncontext
 */

 public javax.ejb.sessioncontext getsessioncontext(){
  return mysessionctx;
 }

 /**
  * setsessioncontext
 */

 public void setsessioncontext(javax.ejb.sessioncontext ctx){
  mysessionctx = ctx;
 }

 /**
  * ejbactivate
 */

 public void ejbactivate(){
 }

 /**
  * ejbcreate
 */
 public void ejbcreate() throws javax.ejb.createexception{
 }

 /**
  * ejbpassivate
 */

 public void ejbpassivate(){
 }

 /**
  * ejbremove
 */
 public void ejbremove(){
 }

 /**
  * 商务方法
 */
 public string hello(){
  return "hello,world! this is created by ejb!";
 }
}

  至此,我们己经将 ejb 类所需的所有的所有类编写好了,下面我们编写用于测试此 ejb 程序的文件,具体来说有两个:一个是用客户端 servlet 来调用 ejb ,另一个是用客户端类来调用 ejb 。实现如下:


  五、实现测试 ejb 的程序

  进入 c:/jboss/myproject/src 目录中,新建一个 helloservlet.java 文件,用于测试通过 servlet 调用 ejb 。内容如下:

package hello;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.*;
import javax.naming.initialcontext;
import javax.naming.context;
import hello.ejb.*;
import java.util.*;

public class helloservlet extends httpservlet
{
 private hellohome home;
 public void init() throws servletexception
 {
  try
  {
   string initctxfactory = getinitparameter(context.initial_context_factory);
   string providerurl = getinitparameter(context.provider_url);
   properties env = new properties();
   if(initctxfactory!=null)
   {
    env.put(context.initial_context_factory,initctxfactory);
   }
   if(providerurl!=null)
   {
    env.put(context.provider_url,providerurl);
   }
   context ctx=new initialcontext(env);
   object objref = ctx.lookup("hello");
   // 主接口
   home=(hellohome)javax.rmi.portableremoteobject.narrow(objref,hello.ejb.hellohome.class);
  }catch(javax.naming.namingexception ne){
   system.out.println("create exception caught:"+ne);
   throw new servletexception(ne.tostring());
  }catch(exception e)
  {
   throw new servletexception(e.tostring());
  }
 }
 
 public void service(httpservletrequest req,httpservletresponse res) throws ioexception
 {
  res.setcontenttype("text/html");
  printwriter out =res.getwriter();
  out.println("<html><head><title>the first ejb</title></head>");
  try{
   // 组件接口
   hello bean =home.create();
   out.println(bean.hello());
  }catch(javax.ejb.createexception ce){
   out.println("create exception caught:"+ce);
   ce.printstacktrace(out);
  }catch(java.rmi.remoteexception re){
   out.println("remote exception caught:"+re);
   re.printstacktrace(out);
  }
  out.println("</body></html>");
 }
}

  同样在此目录中,新建一个 helloclient.java 文件,用于通过客户端类测试 ejb ,内容如下:

package hello.ejb;
import java.util.*;
import java.io.*;
import javax.naming.initialcontext;
import javax.rmi.portableremoteobject;
import javax.naming.context;
import javax.rmi.portableremoteobject;
import hello.ejb.*;

public class helloclient
{
 public static void main(string[] args)
 {
  try
  {
   //jndi 配置 , 应实现为外部属性文件
   properties env = new properties();
   //config.properties 文件应该放在和 hello 包目录所在目录的同级目录中。即它和 hello 文件夹同在一个文件夹中。

   env.load(new fileinputstream("config.properties"));

   // get a naming context
 
   system.out.println(env);
   initialcontext ctx = new javax.naming.initialcontext(env);
   system.out.println("got context");
   // get a reference to the interest bean
   //jboss 默认 jndi 名为 ejb-jar.xml 中的 :ejb-name
   object ref = ctx.lookup("hello");
   system.out.println("got reference");
   // get a reference from this to the bean"s home interface
   hellohome home = (hellohome)portableremoteobject.narrow(ref, hellohome.class);
   // create an hello object from the home interface
   hello hello = home.create();
   // call the hello() method
   system.out.println(hello.hello());
  }catch(exception e)
  {
   system.out.println(e.tostring());
  }
 }
}

  然后,我们还需增加一个此客户端程序运行时所需的 jndi 配置文件。方法如下:

  进入 : c:/jboss/myproject/ejb/client 目录,用记事本新建一个 config.properties 文件,内容如下三行:

java.naming.factory.initial=org.jnp.interfaces.namingcontextfactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://192.168.10.114:1099


  六、 编写服务器端的配置文件,

  首先进入 c:/jboss/myproject/jsp/hello.war/web-inf 目录,用记事本创建一个 web.xml 文件,用于配置 servlet ,内容如下:

<?xml version="1.0" encoding="gb2312"?>
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>helloworld</servlet-name>
<display-name>helloworld</display-name>
<servlet-class>hello.helloworld</servlet-class>
</servlet>
<servlet>
<servlet-name>helloservlet</servlet-name>
<display-name>helloservlet</display-name>
<servlet-class>hello.helloservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<url-pattern>/helloservlet</url-pattern>
</servlet-mapping>
</web-app>

  然后进入 c:/jboss/myproject/ejb/hello.jar/meta-inf 目录,创建一个 ejb-jar.xml 文件,内容如下:

<?xml version="1.0" encoding="gb2312"?>
<!doctype ejb-jar public "-//sun microsystems, inc.//dtd enterprise javabeans 2.0//en"
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<description>this is hello ejb example</description>
<display-name>helloeejb</display-name>
<enterprise-beans>
<session>
<display-name>hello</display-name>
<ejb-name>hello</ejb-name>
<home>hello.ejb.hellohome</home>
<remote>hello.ejb.hello</remote>
<local-home>hello.ejb.hellolocalhome</local-home>
<local>hello.ejb.hellolocal</local>
<ejb-class>hello.ejb.hellobean</ejb-class>
<session-type>stateless</session-type>
<transaction-type>container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>

  在此目录中,再建一个 jboss-service.xml 文件,内容如下:

<?xml version="1.0" encoding="gb2312"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>hello</ejb-name>
<jndi-name>hello</jndi-name>
</session>
<secure>true</secure>
</enterprise-beans>
<reource-managers/>
</jboss>

  七、 到此为止,我们将所有的测试文件和配置文件己编写完成。

  八、 下面我们编写编译这些类文件的脚本程序,进入 c:/jboss/bin 目录下,新建一个 com.bat 文件,内容如下:

set classpath=.;c:/j2sdk1.4.1_01/lib/tools.jar;c:/jboss/client/jbossall-client.jar;c:/jboss/client/jboss-client.jar;c:/jboss/client/jboss-iiop-client.jar;c:/jboss/server/default/lib/jnpserver.jar;c:/jboss/client/jnp-client.jar;c:/jboss/client/log4j.jar;c:/jboss/client/jboss-j2ee.jar; c:/jboss/server/all/lib/javax.servlet.jar

javac %1

  同样在此目录新建一个 runclient.bat ,用于运行客户端程序,内容如下:

set classpath=.;c:/j2sdk1.4.1_01/lib/tools.jar;c:/jboss/client/jbossall-client.jar;c:/jboss/client/jboss-client.jar;c:/jboss/client/jboss-iiop-client.jar;c:/jboss/server/default/lib/jnpserver.jar;c:/jboss/client/jnp-client.jar;c:/jboss/client/log4j.jar;c:/jboss/client/jboss-j2ee.jar; c:/jboss/server/all/lib/javax.servlet.jar

java %1

  五、编译和运行测试程序

  1、 进入 c:/jboss/myproject/src 目录中,运行 :

com *.java

  将产生一系列 .class 文件。

  2、 将产生的 helloworld.class 和 helloservlet.class 拷贝到 c:/jboss/myproject/jsp/hello.war/web-inf/classes/hello 目录中。

  3、 将产生的 hellohome.class 和 helloclient.class 和 hello.class 文件放入 c:/jboss/myproject/ejb/client/hello/ejb 目录中

  4、 将产生的 hello.class 和 hellobean.class 和 hellohome.class 和 hellolocal.class 和 hellolocalhome.class 放入 c:/jboss/myproject/ejb/hello.jar/hello/ejb 目录中。

  至此,我们己经完成了所有类的编译,并将它们放入到适当的地方。

  这时,我们将 myproject 下 ejb 目录中的 hello.jar 目录和 jsp 下的 hello.war 目录拷贝到
c:/jboss/server/all/deploy 下。

  这时启动 jboss 的工作台自动会发布此两个目录。我们便可以测试了。
  
  启动 jboss :

run.bat ?cc all

  用 ie 打开 http://localhost:8080/hello/index.jsp 测试 jsp 文件

  用 ie 打开 http://localhost:8080/hello/helloworld 测试 serlvet

  用 ie 打开 http://localhost:8080/hello/helloservlet 测试用 servlet 调用 ejb

  然后进入

  c:/jboss/myproject/ejb/client 运行:

runclient hello/ejb/helloclient

  成功!

扫描关注微信公众号