当前位置:首页 > 网络编程 > WEB编程 > JSP > JSP与EJB(4)

JSP与EJB(4)

点击次数:29 次 发布日期:2008-11-26 16:28:50 作者:源代码网
源代码网推荐
  附录:一个具体而微的EJB应用实例?

  1.安装Apusic Application Server

  Note:以下以Linux为例,来说明Apusic Application Server的安装过程。其他平台的安装,可参考Apusic Application Server安装手册。

  下载JDK1.2,Apusic Application Server必须运行在JDK1.2以上环境中。可从以下站点下载最新JDK。

  http://Java.sun.com

  下载Apusic Application Server

  Apusic Application Server 试用版可从以下网址得到:

  http://www.apusic.com/download/enter.jsp

  在下载完成后,你可以得到一个包裹文件apusic.zip,选定安装目录,假设安装到/usr下,则用以下命令:

  cd /usr

  jar xvf apusic.zip

  /usr下会出现一个目录apusic,Apusic Application Server的所有程序都被解压到/usr/apusic下。

  将以下路径加入到CLASSPATH中

  /usr/apusic/lib/apusic.jar

  $JAVA_HOME/lib/tools.jar

  用以下命令运行Apusic Application Server

  Java -Xms64m com.apusic.server.Main -root /usr/apusic

  2.定义EJB远程接口(Remote Interface)

  任何一个EJB都是通过Remote Interface被调用,EJB开发者首先要在Remote Interface中定义这个EJB可以被外界调用的所有方法。执行Remote Interface的类由EJB生成工具生成。以下是HelloBean的Remote Inteface程序:

  package ejb.hello;

  import Java.rmi.RemoteException;

  import Java.rmi.Remote;

  import Javax.ejb.*;

  public interface Hello extends EJBObject, Remote {

  //this method just get "Hello World" from HelloBean.

  public String getHello() throws RemoteException;

  }

  3.定义Home Interface

  EJB容器通过EJB的Home Interface来创建EJB实例,和Remote Interface一样,执行Home Interface的类由EJB生成工具生成。以下是HelloBean 的Home Interface程序:

  package ejb.hello;

  import Javax.ejb.*;

  import Java.rmi.Remote;

  import Java.rmi.RemoteException;

  import Java.util>*;

  /**

  * This interface is extremely simple it declares only

  * one create method.

  */

  public interface HelloHome extends EJBHome {

  public Hello create() throws CreateException,

  RemoteException;

  }

  4.写EJB类

  在EJB类中,编程者必须给出在Remote Interface中定义的远程方法的具体实现。EJB类中还包括一些 EJB规范中定义的必须实现的方法,这些方法都有比较统一的实现模版,编程者只需花费精力在具体业务方法的实现上。以下是HelloBean的代码:

  package ejb.hello;

  import Javax.ejb.*;

  import Java.util.*;

  import Java.rmi.*;

  public class HelloBean implements SessionBean {

  static final boolean verbose = true;

  private transient SessionContext ctx; // Implement the methods in the SessionBean

  // interface

  public void ejbActivate() {

  if (verbose)

  System.out.println("ejbActivate called");

  }

  public void ejbRemove() {

  if (verbose)

  System.out.println("ejbRemove called");

  }

  public void ejbPassivate() {

  if (verbose)

  System.out.println("ejbPassivate called");

  }

  /**

  * Sets the session context.

  *

  * @param SessionContext

  */

  public void setSessionContext(SessionContext ctx) {

  if (verbose)

  System.out.println("setSessionContext called");

  this.ctx = ctx;

  }

  /**

  * This method corresponds to the create method in

  * the home interface HelloHome.Java.

  * The parameter sets of the two methods are

  * identical. When the client calls

  * HelloHome.create(), the container allocates an

  * instance of the EJBean and calls ejbCreate().

  */

  public void ejbCreate () {

  if (verbose)

  System.out.println("ejbCreate called");

  }

  /**

  * **** HERE IS THE BUSINESS LOGIC *****

  * the getHello just return a "Hello World" string.

  */

  public String getHello()

  throws RemoteException

  {

  return("Hello World");

  }

  }

  5.创建ejb-jar.xml文件

  ejb-jar.xml文件是EJB的部署描述文件,包含EJB的各种配置信息,如是有状态Bean(Stateful Bean)还是无状态Bean(Stateless Bean),交易类型等。ejb-jar.xml文件的详细信息请参阅EJB规范。以下是HelloBean的配置文件:

  Hello

  ejb.hello.HelloHome

  ejb.hello.Hello

  ejb.hello.HelloBean

  Stateless

  Container

  Hello

  *

  Required

  6.编译和部署

  编译Java源文件并将编译后class和ejb-jar.xml打包到Hello.jar

  mkdir build

  mkdir build/META-INF

  cp ejb-jar.xml build/META-INF

  Javac -d build *.Java

  cd build

  jar cvf Hello.jar META-INF ejb

  cd ..

  用EJB工具生成可部署到Apusic Application Server中运行的jar文件:

  Java com.apusic.ejb.utils.EJBGen -d /usr/apusic/classes/Hello.jar build/Hello.jar

  增加/usr/apusic/classes/Hello.jar到CLASSPATH中

  将Hello.jar加入到Apusic Application Server配置文件中。在/usr/apusic/config/server.xml 加入以下几行:

  classes/Hello.jar

  Hello

  HelloHome

  启动服务器

  Java -Xms64m com.apusic.server.Main -root /usr/apusic

  7.写客户端调用程序

  现在读者可以从Java Client,JSP,Servlet或别的EJB调用HelloBean。

  调用EJB有以下几个步骤:

  ·通过JNDI(Java Naming Directory Interface)得到EJB Home Interface

  ·通过EJB Home Interface 创建EJB对象,并得到其Remote Interface

  ·通过Remote Interface调用EJB方法

  以下是一个从Java Client中调用HelloBean的例子:

  package ejb.hello;

  import Javax.naming.Context;

  import Javax.naming.InitialContext;

  import Java.util.Hashtable;

  import Javax.ejb.*;

  import Java.rmi.RemoteException;

  /**

  * @author Copyright (c) 2000 by Apusic, Inc. All Rights Reserved.

  */

  public class HelloClient{

  public static void main(String args[]){

  String url = "rmi://localhost:6888";

  Context initCtx = null;

  HelloHome hellohome = null;

  try{

  Hashtable env = new Hashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY,

  "com.apusic.jndi.InitialContextFactory");

  env.put(Context.PROVIDER_URL, url);

  initCtx = new InitialContext(env);

  }catch(Exception e){

  System.out.println("Cannot get initial context: " + e.getMessage());

  System.exit(1);

  }

  try{

  hellohome = (HelloHome)initCtx.lookup("HelloHome");

  Hello hello = hellohome.create();

  String s = hello.getHello();

  System.out.println(s);

  }catch(Exception e){

  System.out.println(e.getMessage());

  System.exit(1);

  }

  }

  }

  运行HelloClient,可得到以下输出:Hello World

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华