免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1474 | 回复: 0
打印 上一主题 下一主题

[WebLogic] MyEclipse + Weblogic8.1 开发 EJB(转一篇) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-11 16:02 |只看该作者 |倒序浏览

一、准备工作 安装好eclipse3.1/myeclipse4.1/weblogic8.1/j2sdk1.4(注:Weblogic8.1只能配j2sdk1.4,Weblogic9只能配j2sdk1.5)
二、打开BEA WebLogic Configuration Wizard  -->新建WebLogic配置 -->选择"Basic Weblogic Server Domain" -->选择"快速"

然后按照上图配置好usernme和user password并请牢记。

选择Sun SDK

然后点击 “创建”

二.MyEclipse设置
window-->preferences-->MyEclipse-->Application servers-->webLogic8

按上图配置好,上面WebLogic8的JDK选择jdk1.4





SessionBean的代码为:

1 package com.ejb;

2

3 import java.rmi.RemoteException;

4

5 import javax.ejb.EJBException;

6 import javax.ejb.SessionBean;

7 import javax.ejb.SessionContext;

8


9 /** *//**

10  * XDoclet-based session bean.  The class must be declared

11  * public according to the EJB specification.

12  *

13  * To generate the EJB related files to this EJB:

14  *        - Add Standard EJB module to XDoclet project properties

15  *        - Customize XDoclet configuration for your appserver

16  *        - Run XDoclet

17  *

18  * Below are the xdoclet-related tags needed for this EJB.

19  *

20  * @ejb.bean name="HelloWorld"

21  *           display-name="Name for HelloWorld"

22  *           description="Description for HelloWorld"

23  *           jndi-name="ejb/HelloWorld"

24  *           type="Stateless"

25  *           view-type="remote"

26  */


27 public class HelloWorld implements SessionBean ...{

28


29     /** *//** The session context */

30     private SessionContext context;

31


32     public HelloWorld() ...{

33         super();

34         // TODO 自动生成构造函数存根

35     }

36


37     /** *//**

38      * Set the associated session context. The container calls this method

39      * after the instance creation.

40      *

41      * The enterprise bean instance should store the reference to the context

42      * object in an instance variable.

43      *

44      * This method is called with no transaction context.

45      *

46      * @throws EJBException Thrown if method fails due to system-level error.

47      */

48     public void setSessionContext(SessionContext newContext)


49         throws EJBException ...{

50         context = newContext;

51     }

52


53     public void ejbRemove() throws EJBException, RemoteException ...{

54         // TODO 自动生成方法存根

55

56     }

57


58     public void ejbActivate() throws EJBException, RemoteException ...{

59         // TODO 自动生成方法存根

60

61     }

62


63     public void ejbPassivate() throws EJBException, RemoteException ...{

64         // TODO 自动生成方法存根

65

66     }

67


68     /** *//**

69      * An example business method

70      *

71      * @ejb.interface-method view-type = "remote"

72      *

73      * @throws EJBException Thrown if method fails due to system-level error.

74      */


75     public String hello() throws EJBException ...{

76         // rename and start putting your business logic here

77         return new String("HelloEJBWorld!");

78     }

79

80 }

81

配置属性

添加weblogic.jar。我的路径是d:bea\weblogic90\server\lib\weblogic.jar






配置Createtables,设置为AlterOrCreate,以便以后加入其它EJB组件时共享一个部署描述文件,必须要配置,否则会报错。


version选项设定8.1,这里没有截图



1 package com;
2
3 import java.rmi.RemoteException;
4 import java.util.Properties;
5
6 import javax.ejb.CreateException;
7 import javax.naming.Context;
8 import javax.naming.InitialContext;
9 import javax.naming.NamingException;
10
11 import com.interfaces.HelloWorld;
12 import com.interfaces.HelloWorldHome;
13
14 public class EJBTest {
15
16     /**
17      * @param args
18      */
19     public static void main(String[] args) {
20         // TODO 自动生成方法存根
21         Properties properties=new Properties();
22         properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
23         properties.setProperty(Context.PROVIDER_URL,"t3://localhost:7001");
24         
25         Context context;
26         try {
27             context = new InitialContext(properties);
28             HelloWorldHome hwh=(HelloWorldHome)context.lookup("ejb/HelloWorld");
29             HelloWorld hw=hwh.create();
30             String s=hw.hello();
31             System.out.println(s);
32         } catch (NamingException e) {
33             // TODO 自动生成 catch 块
34             e.printStackTrace();
35         } catch (RemoteException e) {
36             // TODO 自动生成 catch 块
37             e.printStackTrace();
38         } catch (CreateException e) {
39             // TODO 自动生成 catch 块
40             e.printStackTrace();
41         }
42         
43     }
44
45
46 }
47
运行后看到结果如下图



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/5841/showart_275768.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP