当前位置: 代码迷 >> JBoss >> 关于在Servlet中调用EJB 3 stateless session bean的有关问题
  详细解决方案

关于在Servlet中调用EJB 3 stateless session bean的有关问题

热度:7088   发布时间:2013-02-26 00:00:00.0
关于在Servlet中调用EJB 3 stateless session bean的问题
小弟不才,现在在学习EJB 3.0,今天写了一个小程序,是在Servlet中调用stateless session bean的方法。程序代码如下:
远程接口:
package com.ejb;
import javax.ejb.Stateless;
@Stateless
public class Calculator implements CalculatorRemote {
public int add(int i, int j) {
return i + j;
}
}

session bean class:
package com.ejb;
import javax.ejb.Stateless;
@Stateless
public class Calculator implements CalculatorRemote {
public int add(int i, int j) {
return i + j;
}
}

Servlet:
package com.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.naming.*;
import java.util.Properties;
import com.ejb.*;
import javax.rmi.PortableRemoteObject;

public class EJBServlet extends HttpServlet {
public EJBServlet() {
super();
}

public void destroy() {
super.destroy();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Context ctx = null;
try {
ctx = this.getInitialContext();
Object ref = ctx.lookup("Calculator/remote");
CalculatorRemote calculator = (CalculatorRemote) PortableRemoteObject.narrow(ref, CalculatorRemote.class);

int i = Integer.parseInt(request.getParameter("number1"));
int j = Integer.parseInt(request.getParameter("number2"));
Integer result = calculator.add(i, j);
request.setAttribute("result", result);
request.getRequestDispatcher("index.jsp").forward(request, response);
} catch (NamingException e) {
e.printStackTrace();
}
}

public Context getInitialContext() throws NamingException{
Properties pro = new Properties();
pro.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
pro.put(Context.PROVIDER_URL, "jnp://localhost:1099");
return new InitialContext(pro);
}

public void init() throws ServletException {
}
}

web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name> EJBServlet </servlet-name>
<servlet-class> com.web.EJBServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> EJBServlet </servlet-name>
<url-pattern> ejbservlet </url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file> index.jsp </welcome-file>
</welcome-file-list>
</web-app>

这段代码在JBuilder和weblogic环境下运行没有任何问题,但在MyEclipse和JBoss环境中,当部属web项目时(我分别定义了两个项目,一个ejb,一个web),服务器会抛出java.lang.reflect.InvocationTargetException异常。我怀疑是JBuilder自动生成了一些配置,才使得项目得以成功部属,而MyEclipse则没有。小弟在网上找了很长时间不能得到解决问题的答案,特在此请教各位高手,望能帮小弟解决这个问题,非常感谢!

------解决方案--------------------------------------------------------
在session bean的类中添加JBoss特有的annotation:@RemoteBinding(jndiBinding="Calculator/remote"),让JBoss服务器为session bean创建JNDI名称
  相关解决方案