EJB 无状态会话 Bean - 无法创建错误

发布于 11-04 08:19 字数 1994 浏览 6 评论 0原文

首先我要指出的是,虽然我已经使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味2024-11-11 08:19:53

如果这是一个本地 EJB(与您的 servlet 在同一个 JVM/EE 容器中),您可以尝试将您的 EJB 声明为 @LocalBean(在 @Stateless 注释下)。您还可以删除 @Remote 接口(并使您的 EJB 无记录器实现它)。

所以你的 EJB 将变成

@Stateless
@LocalBean
public class SDBSStatelessSessionBean
{
    public String sayHello() 
    {
        return "This seems to be working just fine.";
    }     
}

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

@Stateless
@LocalBean
public class SDBSStatelessSessionBean
{
    public String sayHello() 
    {
        return "This seems to be working just fine.";
    }     
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文