编程语言

img liaojingbin

WebSphere下从helloWorld开始开发EJB

发表于2004/10/29 16:14:00  750人阅读

一、准备工作

1、          ejb容器:WebSphere5.1.2

2、          开发工具:wsad5.1

二、开发helloWorld ejb程序。

1、          Home接口

package ljb.ejb.helloworld;

import java.rmi.RemoteException;

import javax.ejb.EJBHome;

import javax.ejb.CreateException;

 

 

public interface HelloEjbHome extends EJBHome{

  

   HelloEjbObject create() throws CreateException, RemoteException;

 

}

2、             Ejb对象接口

 

package ljb.ejb.helloworld;

 

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface HelloEjbObject extends EJBObject{

       

   public String helloworld() throws RemoteException;

 

}

3、             Bean

/*

 * HelloWorld EJB created by liaojingbin 2004-10-25

 */

package ljb.ejb.helloworld;

import javax.ejb.*;

/**

 * @author ljb

 */

public class HelloBean implements SessionBean{

  

   private int i = 0;

  

   public void ejbCreate(){}

   public void ejbRemove(){}

   public void ejbActivate(){}

   public void ejbPassivate(){}

   public void setSessionContext(SessionContext ctx){}

 

   public String helloworld()

   {

      return new String("HelloWorld");

   }

 

}

4、             使用jsp调用ejb

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>

<HEAD>

<%@ page

language="java"

contentType="text/html; charset=GBK"

pageEncoding="GBK"

import="ljb.ejb.helloworld.*,javax.naming.*,javax.rmi.*"

%>

<META http-equiv="Content-Type" content="text/html; charset=GBK">

<META name="GENERATOR" content="IBM WebSphere Studio">

<TITLE>Hello.jsp</TITLE>

</HEAD>

<BODY>

<%

    InitialContext ict = new InitialContext();

    Object obj = ict.lookup("ejb/ljb/ejb/helloworld/HelloBean");

    HelloEjbHome home = (HelloEjbHome)PortableRemoteObject.narrow(obj,HelloEjbHome.class);

    HelloEjbObject helloobj = home.create();

    out.println(helloobj.helloworld());

%>

 

 

</BODY>

</HTML>

 

5、             web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!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 id="WebApp">

    <display-name>HelloEjbWeb</display-name>

    <servlet>

        <servlet-name>Hello</servlet-name>

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

        <jsp-file>/Hello.jsp</jsp-file>

    </servlet>

    <welcome-file-list>

        <welcome-file>index.html</welcome-file>

        <welcome-file>index.htm</welcome-file>

        <welcome-file>index.jsp</welcome-file>

        <welcome-file>default.html</welcome-file>

        <welcome-file>default.htm</welcome-file>

        <welcome-file>default.jsp</welcome-file>

    </welcome-file-list>

    <ejb-ref id="EjbRef_1098762343451">

        <ejb-ref-name>ejb/ljb/ejb/helloworld/HelloBean</ejb-ref-name>

        <ejb-ref-type>Session</ejb-ref-type>

        <home>ljb.ejb.helloworld.HelloEjbHome</home>

        <remote>ljb.ejb.helloworld.HelloEjbObject</remote>

    </ejb-ref>

</web-app>

6Ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>

<!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 id="ejb-jar_ID">

    <display-name>HelloEjbObject</display-name>

    <enterprise-beans>

        <session id="Session_1098956693725">

            <display-name>HelloEjb</display-name>

            <ejb-name>HelloEjb</ejb-name>

            <home>ljb.ejb.helloworld.HelloEjbHome</home>

            <remote>ljb.ejb.helloworld.HelloEjbObject</remote>

            <ejb-class>ljb.ejb.helloworld.HelloBean</ejb-class>

            <session-type>Stateless</session-type>

            <transaction-type>Container</transaction-type>

        </session>

    </enterprise-beans>

</ejb-jar>

6、          application.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd">

<application id="Application_ID">

    <display-name>HelloEjb</display-name>

    <module id="EjbModule_1098694584637">

        <ejb>HelloEjbEJB.jar</ejb>

    </module>

    <module id="WebModule_1098694584637">

        <web>

            <web-uri>HelloEjbWeb.war</web-uri>

            <context-root>HelloEjbWeb</context-root>

        </web>

    </module>

</application>

 

wsad中测试通过后就可以发布到webshpere上了,下面是发布的步骤,对于新手来说,下面这种方式要简单一些:

1http://localhost:9090/admin/ 进入管理控制台

2在左边的菜单上选择:应用程序/安装新的应用程序(在安装新的应用程序前,你首先要在wsad中将你的企业应用程序导出为一个ear文件)

3、             以下按步骤安装就可以了,注意的是:

绑定企业 Bean JNDI :你的一个bean会绑定到一个jndi名称,它在ejb-jar.xml中指定,一般为:ejb///xxxBean,我的这个是ejb/ljb/ejb/helloworld/HelloBean

映射 EJB 引用到企业 Bean:这是也要指定一个jndi名称:是你的web应用调用的那个企业beanjndi名称,这里ejb/ljb/ejb/helloworld/HelloBean

安装完成后访问

http://localhost:9080/HelloEjbWeb/Hello.jsp

输出:HelloWorld,一切ok

 

阅读全文
0 0

相关文章推荐

img
取 消
img