I wanted to test the functionality of a very simple stateful session bean, but nothing works! The JNDI-Lookup does not work as the bean is not found and when I call the bean by Depency-Inection via @EJB annotation the value is not memorized.
The stateful session bean:
In the above bean, the variable "memory" should retain its value when called from the same browser, eg. by browser refresh.
The client is programmed with @EJB dependency inection. The variable state of memory is lost.
I marked the JNDI-Lookup bold. This results in a javax.naming.NamingExdeption: Lookup failed for...
I opened an error no 181367 at netbeans.
The generated lookup string seems to be correct. The <app-name> is omitted, <module-name> is the -war Archive, the <bean-name> is StatefulBeanAufruf-ejb and the <fully-qualified-interface-name> is ch.geo.bohne.NewSessionBean.
have a look at the examples in EJB 3.1 spec, section 4.4.2. I think the lookup-name should be
In my opinion it's a disadvantage of glassfish (compared with JBoss, for example) that not all JNDI bindings are listed by the administration tool. But if you start glassfish with the verbose option (use startserv.bat in windows) then the global JNDI name of the ejb should is printed to the console.
As an alternative to using a global JNDI name you could also choose the classical solution for servlets: Also note: Stateful session beans shouldn't be injected or initialized as instance variable because a servlet can be used in multiple threads.
SCJP 5 (98%) - SCBCD 5 (98%)
Joined: Dec 19, 2007
it works! But there is still something else which is not clear:
I want to show, that a stateful session bean can store variable values across browser calls. In the following example that does not work.
a) I have the stateful session bean, the member variable intMemory should memorize its value.
b) I have a servlet, which sets variable intMemory to 10.
c) I have an identical servlet, who reads the the variable IntMemory: out.println(newSessionBean.getIntMemory());
Now the problem is, that the value set with the first applet ist not stored when I call it with the reading Applet. I used an Integer in case primitive Variables are not serialized. But this does not help.
Joined: Dec 19, 2007
Designing a shopping cart with a stateful session bean
I found a solution, but it is very inelegant:
The servlet has alzheimer and does not remember the refernce variable of the stateful session bean after a refresh. So you have to save the reference in a HttpSession variable.
The stateful session Bean in wich the state of intMemory should be preserved:
The client Servlet reads the Value of intMemory and adds 10 to it. If yo do not save the reference to the Bean the value will stay 10. If you uncomment the blocks and comment out
it works as expected, the values of the intMemory variables are as expected 10, 20, 30....
It ist still not time to
First: Suppose I have 2 catalogs with goods and one cart (which is the stateful session bean). If the reference to the stateful session bean is unique, it would not be possible to save values from two catalogs (i.e. servlet clients) to the one single bean. The shop could have only one catalog or the reference has to be promoted to another catalog, i.e. in a session
Second: The above solution where the reference variable of the stateful session bean is saved in a HttpSession seems inelegant, somebody knows a better solution?
Third: Can the variable intMemory be an int instead an Integer and still be preserved? (you usually can only save Objects in a session, but here an int is a compound of an object, I will try and report).