Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes EJB and other Java EE Technologies and the fly likes Stateful Session vs Stateless Session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateful Session vs Stateless Session" Watch "Stateful Session vs Stateless Session" New topic
Author

Stateful Session vs Stateless Session

Kiran G Kumar
Greenhorn

Joined: May 01, 2005
Posts: 5
Hi all,
I am a newbie to EJB. Sorry, if this sounds like a redundant enquiry. My doubt is related to the difference between Stateful and Stateless session beans.

I have 3 jsps(numbered j1, j2, j3) and 2 servlets(s1 and s2). The values in j2 and j3 depend on the values selected on j1. I'm using s1 and s2 to write out the values to the persistent source and navigation.

How would you use a stateful session bean in this context. (All the values are specific to the user that is using the current session)?
Would it be a good design to use a stateless session to execute the business processes and then pass its(the beans) instance by setting it in the session across multiple requests?



thanking you.
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
If the bean's information is specific to a single user, you're correct: a stateful session bean would be an appropriate choice. The question you should be asking yourself as a newbie is: "Why should I be using EJB instead of a regular POJO?"

Many folks around here will tell you that EJB is overkill for alot of situations. So be careful you don't end up creating more work for yourself when a more simple solution would be just as good.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Kiran G Kumar
Greenhorn

Joined: May 01, 2005
Posts: 5
hi all,
Nathaniel thanks a lot for your reply. I really appreciate that. But I'm in a situation where its mandatory for me to use EJB. So if you could provide more information to my query I'd be grateful.

thanking you.
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Kiran,


Would it be a good design to use a stateless session to execute the business processes and then pass its(the beans) instance by setting it in the session across multiple requests?

Caching the bean instance in the session object (or in any other way) is definitely not a good design decision. One obvious reason is that your container might passivate or remove that instance and you�ll cache an invalid instance. There are design patterns that suggest caching either the home interface or the ejb handler, but I wouldn�t recommend the second one either (especially with SFSB).
If I understood your problem correctly you need to decide whether to save the session information in the HttpSession object or to use a SFSB. If that�s the case than I would suggest you to use the HttpSession object.
Regards.


I think, therefore I exist -- Rene Descartes
Kiran G Kumar
Greenhorn

Joined: May 01, 2005
Posts: 5
hey valentin,
thanks a lot for your reply. Could you please let me know how would you design the scenario I mentioned in the first thread using EJB's? Or if you could direct me to some example that uses SFSB to access client information across more than one JSP it'd be great.

thanking you.
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Kiran,


example that uses SFSB to access client information across more than one JSP

Usually SFSBs don�t access client information. It�s the client that sends the information to the bean. A good example is the shopping chart sample. Do some searches on the net, you might find some good examples.
As a design strategy you have to understand that it�s always better to decouples business components from the code that uses them. Usually your clients should not be aware about the back end server components. They should not care whether they are talking to an ejb container, web services or corba services. Check the business delegate and service locator design patterns as well.
Regards.
Kiran G Kumar
Greenhorn

Joined: May 01, 2005
Posts: 5
hey valentin,
I appreciate your patience in replying to my thread. I'm sorry abt the previous message that I posted. What I actaully meant was the client sending information to the SFSB. Elucidating the problem... I have a JSP(1) that submits some info to the SFSB somewhere down the line of request and responses say for example in JSP(n)I would want to access the information submitted through JSP(1). I have gone through many shopping cart examples but they all exemplify a single request scenario. If you cld help me out with this I'd be grateful. I hope u understand I'm totally confused about this thing. And I'm sure one example would clear up the mess I'm in.

thanking you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful Session vs Stateless Session
 
Similar Threads
puzzle - water jar
Multi dimensional array
Vanishing Sessions
Passed Part II and III
Block execution