Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区 » EJB 3.0  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 ejb
hl_710





发贴: 1
积分: 0
于 2005-10-22 17:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
EJB练习

一、无状态会话Bean

1、本地接口-HelloHome

文件名:HelloHome.java

package hello.ejb;
/**
* Home interface for Enterprise Bean: Hello
*/
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;
}

2、远程接口-Hello

文件名:Hello.java

package hello.ejb;
/**
* Remote interface for Enterprise Bean: Hello
*/
public interface Hello extends javax.ejb.EJBObject {
public String hello() throws java.rmi.RemoteException;
}

3、Bean实现类-HelloBean

文件名:HelloBean.java

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() {
}

/**
* Remote interface
*/
public String hello() {
return "hello , i am a ejb Bean ! time "
+new java.util.Date();
}

}

4、客户端程序

(1)基于JAVA客户虚拟机

package hello.ejb;

import java.util.*;
import java.io.*;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class HelloClient
{

public static void main(String[] args)
{
try
{
//jndi配置,硬编码到java中,应实现为外部属性文件
Properties p=new Properties();
p.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
p.setProperty("java.naming.provider.url",
"localhost:1099");

//out print jndi配置
//p.list(System.out);

// Get a naming context(产生命名空间上下文)
InitialContext jndiContext = new InitialContextCamera;

// Get a reference to the Interest Bean(在JNDI树上查询hello.ejb.HelloHome接口的实例)
//jboss默认jndi名为ejb-jar.xml中的:ejb-name
Object ref = jndiContext.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(调用本地接口的create()方法生成Hello对象
Hello hello = home.create();

// call the hello() method (调用Hello类中的hello()方法
System.out.println(hello.hello());
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}

(2)基于浏览器-hello.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="javax.naming.*,javax.servlet.*,javax.servlet.http.*,
java.rmi.*,javax.ejb.*,javax.rmi.*,hello.ejb.*" %>
<head>
<title>J2ee 1.4开发示例:hello ejb</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<%
try{
//初始化上下文
Context initial=new InitialContext();
//在查找JNDI树上查找Hello实例
Object objref=initial.lookup("Hello");
//获得到Hello本地接口的远程引用
HelloHome home=(HelloHome)PortableRemoteObject.narrow(objref,HelloHome.class);
//从本地接口生成Hello对象
Hello hello=home.create();
//调用Hello的方法hello(),获得输出结果
out.println("输出结果:");
out.println(hello.hello());
}catch(Exception e){
out.println("发生异常");
e.printStackTrace();
}
%>
</body>
</html>
5、编译源程序

(1)编译EJB,生成批处理文件(server.bat),在dos命令行下执行此批处理文件。要注意类路径的设置。

@echo off
echo ========================================================
echo 请先设置 JBoss Environment
echo .
echo JBOSS_HOME: %JBOSS_HOME%
echo .
echo JAVA_HOME: %JAVA_HOME%
echo ======================================================

echo off

set classpath=.
set classpath=%classpath%;%jboss_home%\client\jboss-j2ee.jar

echo on

%java_home%\bin\javac -classpath %classpath% hello\ejb\*.java

pause

(2)编译客户端-生成批处理文件(client.bat),在dos命令行下执行此批处理文件。要注意类路径的设置。

@echo off
echo ========================================================
echo 请先设置 JBoss Environment
echo .
echo JBOSS_HOME: %JBOSS_HOME%
echo .
echo JAVA_HOME: %JAVA_HOME%
echo ======================================================
echo on

%java_home%\bin\javac -classpath %jboss_home%\client\jboss-j2ee.jar hello\ejb\*.java

pause

6、部署EJB

(1)在jboss安装目录\server\default\deploy\下生成hello.jar目录

(2)把编译好的ejb类文件拷贝到jboss安装目录\server\default\deploy\hello.jar下,注意关于类文件的打包。本例是把生成的Hello.class,HelloHome.class,HelloBean.class拷贝到jboss安装目录\server\default\deploy\hello.jar\hello\ejb目录下。

(2)在jboss安装目录\server\default\deploy\hello.jar\下生成META-INF

(3)编辑ejb部署文件描述符-(ejb-jar.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">
<ejb-jar id="ejb-jar_ID">
<display-name>helloEJB</display-name>
<enterprise-beans>
<session >
<ejb-name>Hello</ejb-name>
<home>hello.ejb.HelloHome</home>
<remote>hello.ejb.Hello</remote>
<ejb-class>hello.ejb.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<session>

</enterprise-beans>
</ejb-jar>
(4)把部署文件描述符文件拷贝到 jboss安装目录\server\default\deploy\hello.jar\META-INF\目录下。

7、部署WEB应用

(1)在jboss安装目录\server\default\deploy\下生成hello.war目录:jboss安装目录\server\default\deploy\hello.war

(2)在jboss安装目录\server\default\deploy\hello.war\下生成WEB-INF目录(注意大小写):jboss安装目录\server\default\deploy\hello.war\WEB-INF

(3)把hello.jsp文件拷贝到 jboss安装目录\server\default\deploy\hello.war目录下。

8、测试

(1)在命令行下测试

1)编辑测试批处理文件(run.bat),在dos命令行下执行此批处理文件。要注意类路径的设置。

@echo off
echo ========================================================
echo 请先设置 JBoss Environment
echo .
echo JBOSS_HOME: %JBOSS_HOME%
echo .
echo JAVA_HOME: %JAVA_HOME%
echo ======================================================

echo off
set classpath=.
set classpath=%classpath%;%jboss_home%\client\jbossall-client.jar
set classpath=%classpath%;%jboss_home%\client\jnet.jar
set classpath=%classpath%;%jboss_home%\client\log4j.jar
echo on

%java_home%\bin\java -classpath %classpath% hello.ejb.HelloClient
pause

2)运行结果

(2)在浏览器测试

1)在地址栏中输入:http://localhost:8080/hello/hello.jsp

2)输出结果:




从Coding Fan到真正的技术专家

话题树型展开
人气 标题 作者 字数 发贴时间
8074 ejb hl_710 6327 2005-10-22 17:54
7482 Re:ejb wzl0917 2 2005-11-03 14:45

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923