WebSphere下从helloWorld开始开发EJB

一、准备工作

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.jspTITLE>

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>HelloEjbWebdisplay-name>

    <servlet>

        <servlet-name>Helloservlet-name>

        <display-name>Hellodisplay-name>

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

    servlet>

    <welcome-file-list>

        <welcome-file>index.htmlwelcome-file>

        <welcome-file>index.htmwelcome-file>

        <welcome-file>index.jspwelcome-file>

        <welcome-file>default.htmlwelcome-file>

        <welcome-file>default.htmwelcome-file>

        <welcome-file>default.jspwelcome-file>

    welcome-file-list>

    <ejb-ref id="EjbRef_1098762343451">

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

        <ejb-ref-type>Sessionejb-ref-type>

        <home>ljb.ejb.helloworld.HelloEjbHomehome>

        <remote>ljb.ejb.helloworld.HelloEjbObjectremote>

    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>HelloEjbObjectdisplay-name>

    <enterprise-beans>

        <session id="Session_1098956693725">

            <display-name>HelloEjbdisplay-name>

            <ejb-name>HelloEjbejb-name>

            <home>ljb.ejb.helloworld.HelloEjbHomehome>

            <remote>ljb.ejb.helloworld.HelloEjbObjectremote>

            <ejb-class>ljb.ejb.helloworld.HelloBeanejb-class>

            <session-type>Statelesssession-type>

            <transaction-type>Containertransaction-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>HelloEjbdisplay-name>

    <module id="EjbModule_1098694584637">

        <ejb>HelloEjbEJB.jarejb>

    module>

    <module id="WebModule_1098694584637">

        <web>

            <web-uri>HelloEjbWeb.warweb-uri>

            <context-root>HelloEjbWebcontext-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

 

你可能感兴趣的:(WebSphere下从helloWorld开始开发EJB)