Jbuilder5+weblogic6.1编写EJB的成功例子
1.安装Jbuilder5 enterprise 到c:\jbuider5 BAS 安装到c:\BAS2.安装weblogic6.1 到C:\bea
weblogic 在C:\bea\wlserver6.1 下
3.配置jbuidler5(否则会发现创建ejb的选项是灰色的)
Tools\Enterprise setup
Application Server
BAS 4.5: 选择BAS安装目录c:\BAS
Weblogic6.0:
weblogic installation directory 选择C:\bea\wlserver6.1
bea home directory 选择 C:\bea
4.编写EJB
package:ejbpro
ejb group name:ejbgroup
包文件:ejbgroup.jar,包含ejb-jar.xml,manifest.mf,weblogic-ejb-jar.xml,以及ejbpro下的所有编译过的类文件
类文件:
1)远程接口
//myFirst.java//remote interface
package ejbpro;
import java.rmi.*;
import javax.ejb.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
public interface myFirst extends EJBObject {
public String message(String str) throws RemoteException;
}
2)Bean类
//myFirstBean.java//bean object
package ejbpro;
import java.rmi.*;
import javax.ejb.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
public class myFirstBean implements SessionBean {
private SessionContext sessionContext;
public void ejbCreate() {
}
public void ejbRemove() throws RemoteException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void setSessionContext(SessionContext sessionContext) throws RemoteException {
this.sessionContext = sessionContext;
}
public String message(String str)throws RemoteException{
System.out.println("Remote Object Received From Client: \""+str+"\""); //打印(从客户端)接收到的字符串。
return "Hello,I'm Remote Object"; //返回一应答字符串。
}
}
3)HOME接口
//myFirstHome.java//remote home
package ejbpro;
import java.rmi.*;
import javax.ejb.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
public interface myFirstHome extends EJBHome {
public myFirst create() throws RemoteException, CreateException;
}
4)客户端javaBean
//Client.java//java Bean
package ejbpro;
/**Client side JavaBean to call EJB
* By TomChen
* 2002-4-26
*/
public class Client {
public String message(){
String receiveFromRemote="Error";
try{
javax.naming.Context initContext=new javax.naming.InitialContext();
Object obj=initContext.lookup("myj2ee"); //远程查找,由名字得到对应的对象。
myFirstHome home=(myFirstHome)javax.rmi.PortableRemoteObject.narrow(obj,myFirstHome.class);
myFirst remote=home.create();
receiveFromRemote=remote.message("Hello,Remote Object!"); //远程方法调用
//System.out.println("Received From Remote Object: \""+receiveFromRemote+"\"");
}
catch(Exception e){
e.printStackTrace();
}
return receiveFromRemote;
}
public static void main(String[] args){
try{
javax.naming.Context initContext=new javax.naming.InitialContext();
Object obj=initContext.lookup("myj2ee"); //远程查找,由名字得到对应的对象。
myFirstHome home=(myFirstHome)javax.rmi.PortableRemoteObject.narrow(obj,myFirstHome.class);
myFirst remote=home.create();
String receiveFromRemote=remote.message("Hello,Remote Object!"); //远程方法调用
System.out.println("Received From Remote Object: \""+receiveFromRemote+"\"");
}
catch(Exception e){
e.printStackTrace();
}
}
}
5.部署EJB
1)将ejbgroup.jar copy 到C:\bea\wlserver6.1\config\mydomain\applications 2)将ejbpro copy 到 C:\bea\wlserver6.1\config\mydomain\applications\DefaultWebApp\WEB-INF\classes
3)启动weblogic default server
进入控制台http://localhost:7001/console
点击左面的操作目录 Deployement 下的EJB,点击右框架中的Install a new EJB...Upload ejbgroup.jar
上传后左边的EJB下出现ejbgroup,点击,在右边的框架中选中 Deployed,点击Apply
finised.
6.编写index.jsp
<html>
<head>
<title>EJB测试 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<jsp:useBean id="ejb" class="ejbpro.Client"/>
<%
String str=ejb.message();
out.println(str);
%>
</body>
</html>
放在C:\bea\wlserver6.1\config\mydomain\applications\DefaultWebApp\下
7.开始测试 http://localhost:7001/index.jsp
有问题联系我chenwpp@21cn.com