Java – how do I get a new stateful session bean in a servlet thread?
I'm experimenting with EJB3
I want to inject a stateful session bean into a servlet so that every user accessing the servlet will get a new bean
Obviously, I can't make a bean an instance variable of a servlet because it will be shared And significant injection of local variables is not allowed
I can use the new operator to create beans, but it doesn't seem to be the right way
Is there a right way to do this? It seems that what I want to do is quite simple. After all, we want every new customer to find an empty shopping cart
Solution
You cannot use new to get a new SFSB
You usually use initialcontext to find new
MyBean bean = (MyBean) new InitialContext().lookup( name );
You will then get a reference to a specific SFSB that can be reused in the request
Start with this answer:
For more information about SFSB, you may be interested in my other answers:
> Stateful EJBs in web application? > Java: Tracking a user login session – Session EJBs vs HTTPSession > Correct usage of Stateful Beans with Servlets
I hope I can help you