本文介绍了一个简单Stateless Session Bean的开发,及其在Jboss下的部署;并分别用java,jsp和servlet做为客户端,测试了这个EJB。

2.1 创建目录结构

在D:\下新建一个myprojects目录,用于存放自己开发的项目;

在myprojects目录下新建1个helloworld目录;

在helloworld目录下新建3个目录:jsp目录,该目录用于存放所有的jsp和html等文件;src目录,该目录用于存放所有的java源文件;build目录,该目录用于存放所有的class文件。

2.2 一个简单的Stateless Session Bean

EJB2.0中,开发一个Stateless Session Bean包括6个步骤:

实现Home接口(HelloHome.java)

实现Remote接口(Hello.java)

实现EJB类(HelloBean.java)

实现Localhome接口(HelloLocalHome.java)

实现Local接口(helloLocal.java)

编写部署文件(ejb-jar.xml)

2.2.1 EJB的详细开发过程

Home接口负责控制一个Bean的生命周期(生成、删除和查找Bean)。我们需要为HelloworldHome接口定义一个create() 方法,用来获得一个实例Bean的引用,返回的对象是Remote接口。HelloHome.java的代码如下:

package com.neptune.helloworld.ejb;

import javax.ejb.EJBHome;

public interface HelloHome extends EJBHome {

public Helloworld create()

throws javax.ejb.CreateException,

java.rmi.RemoteException;

}

Remote接口提供了EJB类的所有业务方法,但具体的方法在EJB类中实现。大部分方法已经在EJBObject中定义,在Remote接口中,我们只需要重申Bean类中的业务逻辑方法。Hello.java的代码如下:

package com.neptune.helloworld.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface Hello extends EJBObject {

public String sayHello() throws RemoteException;

}

EJB类包含了业务逻辑的具体细节。HelloBean.java的代码如下:

package com.neptune.helloworld.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

public class HelloBean implements SessionBean {

private javax.ejb.SessionContext sessionCtx;

public SessionContext getSessionContext(){

return sessionCtx;

}

public void ejbActivate()

throws EJBException, RemoteException {

}

public void ejbPassivate()

throws EJBException, RemoteException {

}

public void ejbRemove()

throws EJBException, RemoteException {

}

public void setSessionContext(SessionContext _sessionCtx)

throws EJBException, RemoteException {

this.sessionCtx = _sessionCtx;

}

public void ejbCreate()

throws javax.ejb.CreateException{

}

public String sayHello(){

String str = "Hello World. This is my first EJB test!";

return str;

}

}

HelloLocalHome.java的代码如下:

package com.neptune.helloworld.ejb;

import javax.ejb.EJBLocalHome;

public interface HelloLocalHome extends EJBLocalHome {

HelloLocal create() throws javax.ejb.CreateException;

}

HelloLocal.java的代码如下:

package com.neptune.helloworld.ejb;

import javax.ejb.EJBLocalObject;

public interface HelloLocal extends EJBLocalObject {

public String sayHello();

}

现在,我们已经完成了所有EJB代码的编写。最后我们编写一个HelloClient.java文件,来调用该EJB,内容如下:

package com.neptune.helloworld;

import com.neptune.helloworld.ejb.HelloHome;

import com.neptune.helloworld.ejb.Hello;

import java.io.FileInputStream;

import java.util.Properties;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import javax.naming.Context;

public class HelloClient {

public String hello(){

try {

// For jndi

  Properties env = new Properties();

  // Config.properties file

  env.put(Context.INITIAL_CONTEXT_FACTORY,

"org.jnp.interfaces.NamingContextFactory");

    env.put(Context.PROVIDER_URL,

"jnp://127.0.0.1:1099");

  InitialContext ctx = new javax.naming.InitialContext(env);

  //Get a reference to the Interest Bean

  Object ref = ctx.lookup("Hello");

  //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

  return hello.sayHello();

  } catch (Exception e) {

   e.printStackTrace();

   return e.toString();

  }

}

public static void main(String[] args){

HelloClient hc = new HelloClient();

System.out.println(hc.hello());

}

}

然后我们在D:\myproject\helloworld\build目录下新建一个build.bat文件,文件的内容如下:

set SRC_HOME=d:\myprojects\helloworld

set classpath=%classpath%;d:\lib\jboss-j2ee.jar

javac %SRC_HOME%\src\com\neptune\helloworld\ejb\*.java -d %SRC_HOME%\build

javac %SRC_HOME%\src\com\neptune\helloworld\ *.java -d %SRC_HOME%\build

通过Run这个build.bat文件,这些java source就被编译成class文件放在d:\myproject\helloworld\build目录下。

2.2.2 EJB的部署

现在我们在jboss -3.2.7\server\default\deploy目录下新建一个helloworld目录,然后在helloworld目下新建一个 hello.jar目录,再在hello.jar目录下新建一个META-INF目录。建好以上3个目录后,我们在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>helloEJB</display-name>

<enterprise-beans>

<session>

<display-name>Hello</display-name>

<ejb-name>Hello</ejb-name>

<home>com.neptune.helloworld.ejb.HelloHome</home>

<remote>com.neptune.helloworld.ejb.Hello</remote>

<local-home>com.neptune.helloworld.ejb.HelloLocalHome</local-home>

<local>com.neptune.helloworld.ejb.HelloLocal</local>

<ejb-class>com.neptune.helloworld.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>

然后,我们把生成的class放到hello.jar目录下。到目前为止,形成的目录结构如下图所示:

2.1.jpg

2.4 测试该EJB程序

2.4.1 通过jsp测试该EJB

helloworld目录下新建一hello.war目录,然后在该目录下新建一hello.jsp文件,内容如下:

<%@ page contentType="text/html; charset=gb2312" language="java" %>

<HEAD>

<TITLE>hello.jsp</TITLE>

</HEAD>

<BODY topMargin=0 marginheight="0">

<DIV align=center style="width: 487; height: 138">

<%

com.neptune.helloworld.HelloClient hc =

new com.neptune.helloworld.HelloClient();

out.println(hc.hello());

%>

</DIV>

</BODY>

</HTML>

启动ApacheJboss,打开IE,在IE中输入:

http://127.0.0.1/hello/hello.jsp

如果网页显示“Hello World. This is my first EJB test!”,则表示测试成功。如果测试不成功,请仔细检查以上开发步骤;阅读<<Win2k下Jboss,Tomcat和Apache的集成>>这篇文章。

2.4.2 通过Run HelloClient类测试该EJB

hello.jar目录下新建一个runclient.bat文件,内容如下:

set JBOSS_CLIENT_PATH=C:\javaApp\jboss-3.2.7\client

set JBOSS_SERV_PATH=C:\javaApp\jboss-3.2.7\server

set classpath=%classpath%;c:\javaApp\j2sdk141\lib\tools.jar;%JBOSS_CLIENT_PATH%\jbossall-client.jar;%JBOSS_CLIENT_PATH%\jboss-j2ee.jar;

java com.neptune.helloworld.HelloClient

Run runclient.bat文件后,console将打印出“Hello World. This is my first EJB test!”。如下图所示:

2.2.jpg

2.4.3 通过servlet测试该EJB

HelloServlet.java的代码如下:

import com.neptune.helloworld.ejb.Hello;

import com.neptune.helloworld.ejb.HelloHome;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

public void doPost(HttpServletRequest request,

HttpServletResponse response)

       throws IOException, ServletException {

     response.setContentType("text/html");

     PrintWriter out = response.getWriter();

     out.println("<hr>");

     out.println("Hello World! This is a Servlet!");

     try {

      Properties env = new Properties();

env.put(Context.INITIAL_CONTEXT_FACTORY,

"org.jnp.interfaces.NamingContextFactory");

        env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");

      InitialContext ctx = new javax.naming.InitialContext(env);

      //Get a reference to the Interest Bean

      Object ref = ctx.lookup("Hello");

      //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

      out.println(hello.sayHello());

      } catch (Exception e) {

       e.printStackTrace();

       out.println(e.toString());

      }

      out.println("<hr>");

     }

public void doGet(HttpServletRequest request,

HttpServletResponse response)

       throws IOException, ServletException {

     doPost(request, response);

    }

}

helloworld目下新建一个hello.war目录,再在hello.war目录下新建一个WEB-INF目录。然后在WEB-INF目录下新建一个web.xml文件用于配置servlet,内容如下:

<?xml version="1.0"?>

<!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>

<description>EJB Test</description>

  <servlet>

<servlet-name>helloServlet</servlet-name>

<servlet-class>HelloServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>helloServlet</servlet-name>

<url-pattern>/helloServlet</url-pattern>

</servlet-mapping>

</web-app>

WEB-INF目录下新建一个classes目录,把生成的HelloServlet.class拷贝到WEB-INF目录。

启动ApacheJboss,打开IE,在IE中输入:

http://127.0.0.1/hello/helloServlet

如果测试成功,网页将显示“Hello World! This is a Servlet! Hello World. This is my first EJB test!”。

作者:蔡晓均

E-mail地址:neptunecai@yahoo.com.cn

版权所有,转摘请注明:摘自www.blogjava.net/neptune