weblogic开发EJB

各个容器开发EJB不太一样,这里使用的是Weblogic8.1和EJB2.0进行演示的。

 

 

1 下载java1.4的EE包

2 设置环境变量

3 设置Weblogic的配置

4 添加weblogic的javaee.jar和weblogic.jar到CLASSPATH 在bea/weblogic81/server/lib中

 

下面的演示使用手工,不使用Eclipse

 

1 编写Bean

<textarea cols="50" rows="15" name="code" class="c-sharp">/** * */ package com; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloWorldBean implements SessionBean { public void setSessionContext(SessionContext ctx) { System.out.println("set session context"); } public void ejbCreate() { System.out.println("ejb create"); } public void ejbRemove() { System.out.println("ejb remove"); } public void ejbActivate() { System.out.println("ejb activate"); } public void ejbPassivate() { System.out.println("ejb passivate"); } public String sayHello(String name) throws RemoteException { return "Hello :" + name; } } </textarea>

 

2 实现接口

<textarea cols="50" rows="15" name="code" class="c-sharp">/** * */ package com; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface HelloWorldHome extends EJBHome { HelloWorldRemote create() throws RemoteException, CreateException; } </textarea>

<textarea cols="50" rows="15" name="code" class="c-sharp">/** * */ package com; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloWorldBean implements SessionBean { public void setSessionContext(SessionContext ctx) { System.out.println("set session context"); } public void ejbCreate() { System.out.println("ejb create"); } public void ejbRemove() { System.out.println("ejb remove"); } public void ejbActivate() { System.out.println("ejb activate"); } public void ejbPassivate() { System.out.println("ejb passivate"); } public String sayHello(String name) throws RemoteException { return "Hello :" + name; } } </textarea>

 

3 创建部署描述符ejb-jar.xml 在META-INF文件夹下

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"&gt; &lt;ejb-jar&gt; &lt;display-name&gt;TestEjb&lt;/display-name&gt; &lt;enterprise-beans&gt; &lt;session&gt; &lt;display-name&gt;helloEJB&lt;/display-name&gt; &lt;ejb-name&gt;helloEJB&lt;/ejb-name&gt; &lt;home&gt;com.HelloWorldHome&lt;/home&gt; &lt;remote&gt;com.HelloWorldRemote&lt;/remote&gt; &lt;ejb-class&gt;com.HelloWorldBean&lt;/ejb-class&gt; &lt;session-type&gt;Stateless&lt;/session-type&gt; &lt;transaction-type&gt;Bean&lt;/transaction-type&gt; &lt;/session&gt; &lt;/enterprise-beans&gt; &lt;/ejb-jar&gt; </textarea>

 

4 创建weblogic的部署weblogic-ejb-jar.xml

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN" "http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd"&gt; &lt;weblogic-ejb-jar&gt; &lt;description&gt;&lt;/description&gt; &lt;weblogic-enterprise-bean&gt; &lt;ejb-name&gt;helloEJB&lt;/ejb-name&gt; &lt;jndi-name&gt;ejb/hello&lt;/jndi-name&gt; &lt;/weblogic-enterprise-bean&gt; &lt;/weblogic-ejb-jar&gt; </textarea>

 

5 编译上面的文件

javac

 

HelloWorldBean.class

HelloWorldRemote.class

HelloWorldHome.class

 

6 使用jar进行打包

jar -cvf TestEjb.jar *.*

 

7 使用weblogic的命令

java weblogic.appc TestEjb.jar

 

客户端访问可以去除HelloWorldBean.class

 

8 部署

 8.1 启动weblogic服务,使用console进行部署。

 8.2 或直接复制到应用程序目录,比如C:/bea/user_projects/domains/mydomain/applications也可以。

 

9 编写客户端

<textarea cols="50" rows="15" name="code" class="c-sharp">import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import com.HelloWorldHome; import com.HelloWorldRemote; /* * Created on 2010-11-12 * * To change the template for this generated file go to * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments */ /** * @author Administrator * * To change the template for this generated type comment go to * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments */ public class TestEjb { public static void main(String[] args) { try { System.out.println("begin ctx"); //得到与远程通信的连接 Hashtable cs = new Hashtable(); cs.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); cs.put(Context.PROVIDER_URL, "t3://rollin:7001"); Context ctx = new InitialContext(cs); System.out.println("----------------------"); HelloWorldHome objRef = (HelloWorldHome) ctx.lookup("ejb/hello"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objRef, HelloWorldHome.class); HelloWorldRemote remote = home.create(); System.out.println(remote.sayHello("mrl")); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.toString()); } } } </textarea>

 

10 javac Test.java

11 java Test

<textarea cols="50" rows="15" name="code" class="c-sharp">C:/Documents and Settings/Administrator/workspace/TestEjb/ejbModule&gt;java TestEjb begin ctx ---------------------- Hello :mrl </textarea>

 

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;2010-11-12 下午14时54分11秒 CST&gt; &lt;Notice&gt; &lt;WebLogicServer&gt; &lt;BEA-000355&gt; &lt;Thread "ListenThread.Default" listening on port 7001, ip address *.*&gt; set session context ejb create</textarea>

 

 

 

12 在容器内访问:该web的lib有EJBClient.jar(TestEjb.jar)文件即可。

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;%@ page import="java.sql.*"%&gt; &lt;%@ page import="java.util.*"%&gt; &lt;%@ page import="javax.naming.*"%&gt; &lt;%@ page import="javax.rmi.PortableRemoteObject"%&gt; &lt;%@ page import="com.HelloWorldHome"%&gt; &lt;%@ page import="com.HelloWorldRemote"%&gt; &lt;% String result = ""; try { System.out.println("begin context"); //得到与远程通信的连接 Hashtable cs = new Hashtable(); cs.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); cs.put(Context.PROVIDER_URL, "t3://rollin:7001"); Context ctx = new InitialContext(cs); System.out.println("----------------------"); HelloWorldHome objRef = (HelloWorldHome) ctx.lookup("ejb/hello"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objRef, HelloWorldHome.class); HelloWorldRemote remote = home.create(); result = remote.sayHello("mrl2"); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.toString()); } finally { out.println("end"); } %&gt; &lt;%=result%&gt; &lt;/body&gt; &lt;/html&gt;</textarea>

 

 

访问该页面2次的情况

 

end Hello :mrl2

end Hello :mrl2

<textarea cols="50" rows="15" name="code" class="c-sharp">begin context ---------------------- begin context ----------------------</textarea>

 

容器内其实容器可以发现EJB对象

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;%@ page import="java.sql.*"%&gt; &lt;%@ page import="java.util.*"%&gt; &lt;%@ page import="javax.naming.*"%&gt; &lt;%@ page import="javax.rmi.PortableRemoteObject"%&gt; &lt;%@ page import="com.HelloWorldHome"%&gt; &lt;%@ page import="com.HelloWorldRemote"%&gt; &lt;% String result = ""; try { System.out.println("begin context ag"); //得到与远程通信的连接 Context ctx = new InitialContext(); System.out.println("----------------------"); HelloWorldHome objRef = (HelloWorldHome) ctx.lookup("ejb/hello"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow( objRef, HelloWorldHome.class); HelloWorldRemote remote = home.create(); result = remote.sayHello("mrl3"); } catch (Exception ex) { ex.printStackTrace(); System.out.println(ex.toString()); } finally { out.println("end"); } %&gt; &lt;%=result%&gt; &lt;/body&gt; &lt;/html&gt;</textarea>

 

后台

<textarea cols="50" rows="15" name="code" class="c-sharp">begin context ---------------------- set session context ejb create begin context ag ----------------------</textarea>

 

 

 

你可能感兴趣的:(exception,weblogic,String,session,ejb,import)