我有一个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
答案 0 :(得分:3)
我不确定你是否正确设置了你的有状态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,然后再为他创建一个新的。