I'm sure this is a much asked question, but I have just tried to put a Stateful bean using EJB3.0 into practice and have had some difficulties understanding their point!
Some background:
I'm writing a simple webapp to allow a user to register their details, redirect to a 'confirmation' page, then if they click 'submit' the details are upload to a database. Then the user can return to the login page and login as an existing user, this is being done via EJB3.0 (obviously) and good ol fashioned JSP/Servlets (none of that
Struts or
JSF)... Simple!
The issue...
Initially I used a standard
Java bean, from the registration
JSP to the
Servlet, populated the bean vis 'request.getParameter', then put the bean into the HttpSession.
redirected to the confirmation page, click 'submit' and redirect to the Servlet again.
Got thye bean back from the HttpSession and populated an EntityBean with the detals, which in turn updated the database.
All worked fine!
Then I thought "Surely I should be using an
EJB Stateful bean not a standard java bean in the HttpSession?"
So I changed my codce, created a Stateful bean, populated that from the request.getParameter, redirected to the confirmation page, click 'submit' back to the Servlet and....
Lots and errors about NULL values!
Now my understanding was that a stateful bean remained intact for the duration of the session unless 'destroyed', so I was expecting the Servlet to natrually pickup the bean still containing the data provided from the previous JSP, but it seems (I'm guessing) that at the point of redirecting from the confirmation page back to the Servlet I'm creating a NEW Stateful bean, therefore the values for name, email etc are all NULL, rather than collectinig the existing bean.
Which begs my question..
How would I 'grab' my existing Stateful bean? Why would I use a Stateful bean over my original solution of simply stickinng a standrad Java bean into the HttpSession (and using session.removeAttribute when I'm done)?
Many tanks in advance...