且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在servlet线程中获取新的有状态会话bean?

更新时间:2023-12-03 23:01:16

您不能使用 new 来获取新的SFSB。

You can't use new to get a new SFSB.

你通常做的是查找使用 InitialContext

MyBean bean = (MyBean) new InitialContext().lookup( name );

然后,您将获得对可以在请求中重复使用的特定SFSB的引用。

You get then a reference to a specific SFSB that you can reuse across requests.

来自这个答案


你通常不应该注入SFSB,
除非它进入另一个SFSB或
a Java EE客户端。你应该在引用类(例如你的
servlet)上使用@EJB
来声明ejb-ref和
然后在代码中进行JNDI查找以获得实例
。然后可以将此实例
直接放在
Http会话中。

You should not typically inject SFSB, unless it is into another SFSB or into a Java EE client. You should use @EJB on the referencing class (e.g. your servlet) to declare the ejb-ref and then do a JNDI lookup in the code to obtain the instance. This instance could then be placed directly in your Http session.

有关SFSB的更多信息,可能对我的其他答案感兴趣:

For more information about SFSB, you might be interested in these other answers from me:

  • Stateful EJBs in web application?
  • Java: Tracking a user login session - Session EJBs vs HTTPSession
  • Correct usage of Stateful Beans with Servlets

希望有所帮助。