欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

servlet中调用EJB

程序员文章站 2024-02-22 22:00:46
...
对于初学像我这样初学EJB的菜鸟来讲,EJB的配置是一件非常繁琐的事情,不堪回首啊。呵呵,今天终于明白了昨天晚上遇到的一个白痴错误,所以将一个简单的例子写下来。
我这个例子的前提是EJB已经发布成功,在servlet中对已经发布的EJB的调用。
首先已要做的就是将已发布的EJB组件的jar文件拷贝到当前Web工程的lib下,其目录结构为:
EJB端:
EJBFolderName
|
|------classes
| |
| |-------hello
| | |---------hello.class //Remote
| | |---------helloHome.class //Home
| | | ---------helloBean.class //Bean
| |
| |--------META-INF
| |------ejb-jar.xml //EJB配置文件
| |------MANIFEST.MF //Manifest-Version: 1.0 Class-Path:
|
|-----------src
|
|-------hello
| |---------hello.java //Remote
| |---------helloHome.java //Home
| | ---------helloBean.java //Bean
|
|--------META-INF
|----ejb-jar.xml //servlet配置文件
|-----MANIFEST.MF //Manifest-Version: 1.0 Class-Path:

servlete端:
ServletFolderName
|
|----------src
| |-------hello
| | |--------TestEJB.java //被Servlet(只为目标清晰,也可直接写到Servlet内)
| |
| |-------TestEJBServlet.java //一个Servlet 调用 包hello 中的方法。
|
|
|----------WebRoot

|----------META-INF
| |--------MANIFEST.MF //Manifest-Version: 1.0 Class-Path:
|
|----------WEB-INF
|----------classes
| |-----------TestEJBServlet.class
| |
| | -----------hello | |----------TestEJB.class
|-----------lib
| |----------[color=red]FirstEJB.jar //EJB打好的一个Jar包!EJB2.0不可缺少 [/color]

|
|------------web.xml //Servlet配置文件

基本文件结构就是这样。结束!

*/下面是针对每个文件的详细说明://首先是Servlet端,Servlet有一个servlet文件,实现doGet方法和doPost方法,还有一个Servlet配置文件 web.xml
//TestEJBServlet.java:
import java.io.IOException;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import hello.TestEJB;

public class TestEJBServlet extends HttpServlet {


protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {

this.doPost(request,response); //直接以页面的形式调用Servlet 调用的事doGet方法,

//也可以直接在doGet方法中调用EJB

}

public void doPost(HttpServletRequest request, HttpServletResponse response){

try{

new TestEJB("iiop://localhost:2809/").test(); //调用 TestEJB.java 中的test()方法。

} catch(Exception e){

e.printStackTrace();

}


}

}



//TestEJB.java:

package hello;

import javax.naming.Context;

import java.util.Properties;

import javax.naming.NamingException;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

public class TestEJB {

private static final String JNDI_NAME = "HelloEJB"; //EJB上传websphere时用的JNDI Name

private String url;

private HelloHome home;

public TestEJB(String url) throws NamingException{

this.url = url;

home = lookUpHome();

}

private Context getInitialContext() throws NamingException {

Properties prop = new Properties();

prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

prop.put(Context.PROVIDER_URL, url);

return new InitialContext(prop);

}

private HelloHome lookUpHome() throws NamingException{

Context ctx = getInitialContext();

Object home = ctx.lookup(JNDI_NAME);

return (HelloHome)PortableRemoteObject.narrow(home,HelloHome.class);

}

public void test() throws RemoteException,CreateException,NamingException {

Hello hello = (Hello)PortableRemoteObject.narrow(home.create(),Hello.class);

System.out.println(hello.sayHello());

}

/*public static void main(String args[]) throws Exception{

try{

new TestEJB("iiop://localhost:2809/").test();

}catch(Exception e){

e.printStackTrace();

}

}*/

}


//Servlet 配置文件 web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>TestEJB</servlet-name>

<servlet-class>TestEJBServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>TestEJBServlet</servlet-name> //Servlet映射到web服务器上的名字

<url-pattern>/servlet/TestEJBServlet</url-pattern>//Servlet映射到web服务器上的路径

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>//默认开始页面(可有可无)

</welcome-file-list>

</web-app>


接下来是EJB端:三个java文件(Home.java,Remote.java,Bean.java)和一个ejb-jar配置文件
helloHome.java(就是Home.java 本地接口)

package hello;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;


public interface HelloHome extends EJBHome {

public Hello create()throws CreateException,RemoteException;

}


hello.java(就是Remote.java 远程接口)

package hello;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface Hello extends EJBObject {

public String sayHello()throws RemoteException;

}


helloBean.java(就是Bean.java 实现类)

package hello;
import java.rmi.RemoteException;

import javax.ejb.EJBException;

import javax.ejb.CreateException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

public class HelloBean implements SessionBean {

private SessionContext ctx;

private String words ;

public String sayHello(){

System.out.println("i am in an EJB server."+words);

return words;

}

public void ejbCreate() throws CreateException{

this.words = "hello world";
}
public void ejbActivate() throws EJBException, RemoteException {

// TODO Auto-generated method stub
}

public void ejbPassivate() throws EJBException, RemoteException {

// TODO Auto-generated method stub
}

public void ejbRemove() throws EJBException, RemoteException {

// TODO Auto-generated method stub
}


public void setSessionContext(SessionContext arg0) throws EJBException,

RemoteException {
this.ctx = arg0;
}
}


还有一个配置文件ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar>

<display-name>HelloEjb</display-name>

<enterprise-beans>

<session>

<ejb-name>FirstEJB</ejb-name>

<home>hello.HelloHome</home>

<remote>hello.Hello</remote>

<ejb-class>hello.HelloBean</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>

</session>

</enterprise-beans>

<assembly-descriptor>

<container-transaction>

<method>

<ejb-name>FirstEJB</ejb-name>

<method-intf>Remote</method-intf>

<method-name>*</method-name>

</method>

<trans-attribute>Required</trans-attribute>

</container-transaction>

</assembly-descriptor>

</ejb-jar>

最后还想说一下对EJB的调用的集中方式,主要有三种:
1、用property帮助设置环境

Properties properties = System.getProperties();

properties.put(Context.PROVIDER_URL, "iiop://localhost:2809");
properties.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
Context ctx = new InitialContext(properties);

2、用Hashtable帮助设置环境

Hashtable environment = new Hashtable();

environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

environment.put(Context.PROVIDER_URL, "iiop://localhost:2809"); //可选,同“corbaloc:iiop:localhost:2809”的效果一样。

Context ctx= new InitialContext(environment);

3、WAS默认的方法

Context ctx = new InitialContext();
Object ref = ctx.lookup("ejb/TestEJB");
TestEJBHome home = (TestEJBHome) PortableRemoteObject.narrow(ref, TestEJBHome.class);

testinst = home.create();
arg1.getWriter().println(testinst.testEJB());
System.out.println(testinst.testEJB());