EJB 问题 - 有状态会话 Bean 和 Servlet

发布于 2024-12-23 16:02:03 字数 2916 浏览 1 评论 0原文

我有一个 servlet 代码,它调用 ejb 有状态会话 bean 代码,如下所示,

public class UsesBeansSF extends HttpServlet {   
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
          // do something
       }

    finally {}
 } 

private SessionBeanSFRemote lookupSessionBeanSFRemote() {
    try {
        Context c = new InitialContext();
        return (SessionBeanSFRemote) c.lookup("java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

}

该代码在没有 * 标记之间的情况下运行良好。但是,当我添加 SessionBeanSFRemote sessionBeanSF = LookupSessionBeanSFRemote() 这行(意味着调用有状态会话 Bean)时,代码给出错误。实际上,我必须调用无状态会话 bean 才能执行某些工作。谁能帮助我为什么会发生这种情况?提前致谢。

错误消息如下:

类型异常报告消息

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

异常

javax.servlet.ServletException: PWC1392: Error instantiating servlet class websSF.comsSF.UsesBeansSF

根本原因

com.sun.enterprise.container.common.spi.util.InjectionException: 
         Error creating managed object for class websSF.comsSF.UsesBeansSF

根本原因

java.lang.reflect.InvocationTargetException

根本原因 >

java.lang.RuntimeException: javax.naming.NamingException: 
Lookup failed for 'java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote' in SerialContext  
[Root exception is javax.naming.NamingException: 
ejb ref resolution error for remote business interfaceejbSF.SessionBeanSFRemote [Root exception is java.lang.NullPointerException]]

根本原因

javax.naming.NamingException: 
Lookup failed for 'java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote' in SerialContext  
[Root exception is javax.naming.NamingException: 
ejb ref resolution error for remote business interfaceejbSF.SessionBeanSFRemote
[Root exception is java.lang.NullPointerException]]

根本原因

javax.naming.NamingException: ejb ref resolution error for remote business 
interfaceejbSF.SessionBeanSFRemote [Root exception is java.lang.NullPointerException]

根本原因

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.

服务器:GlassFish Server 开源版 3.0.1

I have a servlet code which calls a ejb stateful session bean code as follows,

public class UsesBeansSF extends HttpServlet {   
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
          // do something
       }

    finally {}
 } 

private SessionBeanSFRemote lookupSessionBeanSFRemote() {
    try {
        Context c = new InitialContext();
        return (SessionBeanSFRemote) c.lookup("java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

}

This code works well without the line between * marks. However, when I am adding SessionBeanSFRemote sessionBeanSF = lookupSessionBeanSFRemote() this line (means calling a Stateful Session Bean), the code is giving error. Actually, I have to call the stateless session bean in order to perform some job. Can anybody help me why it is happening ? Thanks in advance.

Error message is following:

type Exception report message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: PWC1392: Error instantiating servlet class websSF.comsSF.UsesBeansSF

root cause

com.sun.enterprise.container.common.spi.util.InjectionException: 
         Error creating managed object for class websSF.comsSF.UsesBeansSF

root cause

java.lang.reflect.InvocationTargetException

root cause

java.lang.RuntimeException: javax.naming.NamingException: 
Lookup failed for 'java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote' in SerialContext  
[Root exception is javax.naming.NamingException: 
ejb ref resolution error for remote business interfaceejbSF.SessionBeanSFRemote [Root exception is java.lang.NullPointerException]]

root cause

javax.naming.NamingException: 
Lookup failed for 'java:global/MyEJBSF/SessionBeanSF!ejbSF.SessionBeanSFRemote' in SerialContext  
[Root exception is javax.naming.NamingException: 
ejb ref resolution error for remote business interfaceejbSF.SessionBeanSFRemote
[Root exception is java.lang.NullPointerException]]

root cause

javax.naming.NamingException: ejb ref resolution error for remote business 
interfaceejbSF.SessionBeanSFRemote [Root exception is java.lang.NullPointerException]

root cause

java.lang.NullPointerException
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.

Server: GlassFish Server Open Source Edition 3.0.1

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

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

发布评论

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

评论(1

回首观望 2024-12-30 16:02:03

我不确定您是否已正确设置 Stateful bean。你可以尝试这个:

@Stateful(mappedName = "ejb/myStatefulBean")
public class MyStatefulBean implements MyStatefulRemoteInterface {
   // Your implementation
}

然后你可以用以下命令查找它:

InitialContext context = new InitialContext();
MyStatefulRemoteInterface myStatefulBean = (MyStatefulRemoteInterface) context.lookup("ejb/myStatefulBean");

此外,这个有状态bean应该保存到每个客户端的会话中以供重复使用:

HttpSession clientSession = request.getSession(false);
clientSession.setAttribute("myStatefulBean", myStatefulBean);

在将来的请求中,你可以尝试先从客户端的会话中获取bean,然后再创建一个给他新的。

I'm not sure if you have properly set up your Stateful bean. You can try this:

@Stateful(mappedName = "ejb/myStatefulBean")
public class MyStatefulBean implements MyStatefulRemoteInterface {
   // Your implementation
}

then you can look it up with:

InitialContext context = new InitialContext();
MyStatefulRemoteInterface myStatefulBean = (MyStatefulRemoteInterface) context.lookup("ejb/myStatefulBean");

Besides, this stateful bean should be saved into each client's session for re-using:

HttpSession clientSession = request.getSession(false);
clientSession.setAttribute("myStatefulBean", myStatefulBean);

In future requests, you can try to get the bean from the client' session first before creating a new one for him.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文