EJB 无状态会话 Bean - 无法创建错误
首先我要指出的是,虽然我已经使用 Java SE 一段时间了,但这是我第一次涉足 Java EE 领域。我使用 Netbeans 6.9 和 Netbeans 代码生成器来完成大部分繁重的工作(下面会详细介绍)。 GlassFish 的版本是 3 - 下载 Netbeans 时附带的 bog 标准版本。
我创建了一个无状态会话 Bean 以返回一个简单的字符串,如下所示:
@Stateless
public class SDBSStatelessSessionBean implements SDBSStatelessSessionBeanRemote {
@Override
public String sayHello() {
return "This seems to be working just fine.";
}
}
接口定义为:
@Remote
public interface SDBSStatelessSessionBeanRemote {
String sayHello();
}
类和接口都是使用 Netbeans 提供的“插入代码”功能创建的。我认为这样可以避免犯任何愚蠢的新手错误(哦,讽刺的是)。
我的问题是,当我尝试从 servlet 调用 Enterprise bean(该调用是使用 Netbeans 代码生成器中的“调用 Enterprise Bean”选项添加的)时,出现以下错误:
javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
异常是造成的: NoClassDefFoundError
这就是 servlet 进行调用的方式:
@EJB
private SDBSStatelessSessionBeanRemote sDBSStatelessSessionBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Test Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>" + sDBSStatelessSessionBean.sayHello() +"</p>");
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
out.println("<p>" + e.getMessage() + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
恐怕在广泛的谷歌搜索后我还没有找到这个问题的解决方案(主要是因为少数论坛帖子似乎接近这个)问题包含太多行话,我无法遵循解决方案)。
我非常感谢任何为我指明正确方向的建议/帮助。
Let me start by pointing out that while I've been using Java SE for a while now this is my first foray into Java EE territory. I'm using Netbeans 6.9 and the Netbeans code generator to do most of the heavy lifting (more on this further down). The version of GlassFish is 3 - the bog standard one that ships when you download Netbeans.
I have created a stateless Session Bean to return a simple String as follows:
@Stateless
public class SDBSStatelessSessionBean implements SDBSStatelessSessionBeanRemote {
@Override
public String sayHello() {
return "This seems to be working just fine.";
}
}
with the interface definition as:
@Remote
public interface SDBSStatelessSessionBeanRemote {
String sayHello();
}
The class and the interface were both created by using the 'Insert Code' feature that Netbeans provides. I figure this way I avoid making any stupid newbie errors (oh the irony).
My problem is that when I try to call the Enterprise bean from a servlet (the call was added using the 'Call Enterprise Bean' option from the Netbeans code generator) I get the following error:
javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
the exception was caused by:
NoClassDefFoundError
This is how the servlet makes the call:
@EJB
private SDBSStatelessSessionBeanRemote sDBSStatelessSessionBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Test Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>" + sDBSStatelessSessionBean.sayHello() +"</p>");
out.println("</body>");
out.println("</html>");
} catch (Exception e) {
out.println("<p>" + e.getMessage() + "</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
I'm afraid that I have not been able to find a solution to this problem after extensive Googling (mostly because the few forum posts that seem to come near this problem contain too much jargon for me to follow the solution).
I'd greatly appreciate any advice/help pointing me in the right direction.
如果这是一个本地 EJB(与您的 servlet 在同一个 JVM/EE 容器中),您可以尝试将您的 EJB 声明为
@LocalBean
(在@Stateless
注释下)。您还可以删除@Remote
接口(并使您的 EJB 无记录器实现它)。所以你的 EJB 将变成
If this is a local EJB (in the same JVM / EE container as your servlet) you could try declaring your EJB as a
@LocalBean
(under the@Stateless
annotation). You could also remove the@Remote
interface (and make your EJB no loger implement it).So your EJB would become