首先需要配置好eclipse。我這里主要說明weblogic的配置。

注意JDK選擇JDK5.0的版本。
順便將weblogic8的配置也貼上來,供大家比較參考

注意weblogic8的JDK版本必須是JDK1.4。
接下來就開始我們的開發(fā)了。





下面就是SessionBean的代碼

其實(shí)就是修改了其中的一個(gè)方法:
1 /**
2 * An example business method
3 *
4 * @ejb.interface-method view-type = "remote"
5 *
6 * @throws EJBException Thrown if method fails due to system-level error.
7 */
8 public String hello() throws EJBException {
9 // rename and start putting your business logic here
10 return new String("HelloEJBWorld!");
11 }
注意:代碼中的解釋文字不要?jiǎng)h除,因?yàn)閄Doclet需要。2 * An example business method
3 *
4 * @ejb.interface-method view-type = "remote"
5 *
6 * @throws EJBException Thrown if method fails due to system-level error.
7 */
8 public String hello() throws EJBException {
9 // rename and start putting your business logic here
10 return new String("HelloEJBWorld!");
11 }
配置屬性

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










就下來寫EJBTest類:
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 自動(dòng)生成方法存根
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 自動(dòng)生成 catch 塊
34 e.printStackTrace();
35 } catch (RemoteException e) {
36 // TODO 自動(dòng)生成 catch 塊
37 e.printStackTrace();
38 } catch (CreateException e) {
39 // TODO 自動(dòng)生成 catch 塊
40 e.printStackTrace();
41 }
42
43 }
44
45
46 }
47
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 自動(dòng)生成方法存根
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 自動(dòng)生成 catch 塊
34 e.printStackTrace();
35 } catch (RemoteException e) {
36 // TODO 自動(dòng)生成 catch 塊
37 e.printStackTrace();
38 } catch (CreateException e) {
39 // TODO 自動(dòng)生成 catch 塊
40 e.printStackTrace();
41 }
42
43 }
44
45
46 }
47
最后就是看結(jié)果了,先啟動(dòng)weblogic,然后運(yùn)行EJBTest程序。
