I posted a question couple of weeks before regarding the use of Stateful
session bean as a shopping cart.
I got couple of responses saying it is not good idea to use Stateful to
store shopping cart items.
I agree with that.
But in those responses, I found following thoughts.
"Session beans should not be used outside of request
" It is a mistake to use stateful session beans to maintain a client's
That is the purpose of the HttpSession object, not stateful session beans. "
My question is,
What is the use of Stateful session bean then ?
All comments are appreciated.
I find these responses hilarious :)
Stateful session beans should only be used when specific client state is involved. Otherwise, stateless session beans can achieve the same effect with many performance improvements.
Some people don't like to use stateful session beans (many don't like to use entity beans) at all, but if you do use them then a shopping cart is a good thing to implement with them. This is, IMHO, the most classical use of stateful session beans.
I see no reason not to use a session bean for a shopping cart unless it should be persisted over sessions (like in Amazon, for example).
Putting the shopping cart data in an HTTP session does not work with the common MVC architecture, where session beans play the role of controllers. The session bean can't gain access to the HTTP session.
A good example for using stateful session beans for shopping carts is the Blueprints - the Java Pet Store application. Every practice in this application has been carefully considered, so I doubt anyone can say it is simply "a mistake" :)
I agree with Gal.
If you are using n-tier architecture, I would not use an HttpSession for maintaining state. That is the purpose of a Stateful Session Bean. The Business Tier should have no knowledge of the presentation tier. So you should not pass the HttpSession from the presentation to the business tier. If you do this, you reduce the reuseability of your business tier objects. They can only be used for Http systems (see Core J3EE Design Patterns by Sun).
So I would use a Stateful Session Bean. In every book I read, it always gives the Shopping Cart example for s Sateful Bean.
I know iam a bit late on the discussion.
But i have a basic doubt here.
How can i ever use a Stateful session bean to store the state of a client.
Let me give the case i have in hand.
1.A page with 5 or more tabs.
2.The user should have provision to navigate across the pages without having to lose the data.
3.The difficulty i faced in using Stateful session bean was that , i did not know how to get the reference of the session bean used in one tab page from another.
Any suggestions are welcome .
First of all think wether for such kind of applications
(UR case Shopping Cart) ,is EJb really needed?
N Y R U using the EJB if it is not needed.
This shopping cart application can B designed without using the ejb.Of course maintaing MVC architecture(using JSps as front end Servlet as a controller and simple java utility classes as model).Here U can use HttpSession N maintain the client state.
Any way if UR Application really needs EJB then Check
whether USing session at server side is better or using at the client side.
So if U go for server side then i think ,2 maintain Client specific state it is ideal 2 use stateful session beans.
As it would B helpful in maintaing client specif state N also the business logic interacting with the entity beans(here in UR applcn may B Product data Entity bean).
N if U go 4 client side then HttpSession is used.
Hello. (response to Shiv).
A shopping cart is not an application. It is a component. An application like an on-line store (say, Amazon) can be very complex, even enogth for EJB (and there aren't too many such domains...) :)
I didn't say session beans are the only way to implement a shopping cart, I said it's a feasible solution.
When you do use EJB, my intention was that it is not possible to use MVC in the speific way that most people use it, where session beans are the controllers. I said that in my post. Here you can't use HttpSession.
HttpSession does not hold information at the "client side". It holds the information on the server. I'm not what exactly do you mean... care to explain?
Ok friend it is shopping cart component.
N I didnt say that HttpSession stores session at client side,but 2 maintain a session at client side
U have 2 use Httpsession,Cookies,URL Reqriting or hidden form fields depending on UR application.
N if using at the serverside then Session beans wud B used.
You don't want to use a Session bean for a shoppingchart, because you may need to save your shopping chart, when someone wants to come back later.
Why would anyone suggest that a stateful session bean should not be used for a shopping cart? If there is any actual applicable (excuse spelling) rease to use a stateful session bean, IT IS the shopping cart!!!!! If you are using an application server any way, then use the functionality of EJB. I would agreee that I would probably not purchase a very expensive application server if all i wanted to do was have a shopping cart, but I would assume that this is just one aspect of the application.
Remember the key to EJB is the word Enterprise!!! It means that the beans you create today can and SHOULD be used through out the enterprise, if you dont plan on using the business logic coded in your EJB's throughout the enterprise then drop the E and do them as JavaBeans, save yourself some money.
Thanks for your responses.
Here is the link to see the responses which I would like discuss.