4

Is there any relationship between stateful session bean and HTTP session ? What are the use cases where we would require a stateful session bean and what use cases requires HTTP Session. Can i expose a stateful session bean as a restful web service ?

3
  • 4
    You are mixing bat and battle becuase both have "bat" in common
    – Freak
    Jun 21, 2013 at 4:45
  • Freak thanks for your answer,I understood that http session state and session state of a java bean don't have anything in commong.Do you mind giving me use cases when would you use a http session bean vs a stateful session bean please
    – jhon.smith
    Jun 21, 2013 at 5:27
  • I think You still didn't understand it well Because the main thing is that they both have different purposes. Anyway see the edited answer
    – Freak
    Jun 21, 2013 at 5:40

1 Answer 1

4

HTTP is a stateless protocol Which means that it is actual transport protocol between the server and the client -- is "stateless because it remembers nothing between invocations
Now First read this what is HTTPSession and what is Session Bean (keep in mind that session beans are use to maintain the data state across multiple request so mostly a session bean is a stateful session bean because it holds data across whole session)

HTTP Session
An HttpSession object can hold conversational state across multiple requests from the same client. In other words, it persists for an entire session with a specific client. We can use it to store everything we get back from the client in all the requests the client makes during a session.
Session Bean from wiki
In the Java Platform, Enterprise Edition specifications, a Session Bean is a type of Enterprise Bean.A session bean performs operations, such as calculations or database access, for the client. Although a session bean can be transactional, it is not recoverable should a system crash occur. Session bean objects either can be stateless or can maintain conversational state across methods and transactions. If a session bean maintains state, then the EJB container manages this state if the object must be removed from memory. However, the session bean object itself must manage its own persistent data.

In Simple words
Session tracking is the process of maintaining information, or state, about Web site visitors as they move from page to page. It requires some work on the part of the Web developer since there's no built-in mechanism for it. The connection from a browser to a Web server occurs over the stateless Hypertext Transfer Protocol (HTTP)
AND
SFSB's are designed for managed client state over multiple calls to the same session bean (i.e. a conversation). If you look at JBoss Seam, you will see that there is very heavy use of SFSB's for conversation context.
In EJB3, there is no such thing as "stateless is better than stateful session beans". For example, one provides a service like a credit card processor (stateless) and one provides processing for a multi-screen wizard use case (stateful).

In My opinion Managing state using HttpSession and stateless session beans is very difficult and problematic.

EDIT: HTTPSession is use to keep the session tracking like A user session
For example You want to create a Login , Logout mechanism then You must need the HTTPSession because when The user will start navigation between different pages then this HTTPsession will remember that WHO is asking for the pages otherwise it is not possible(because HTTP is a stateless protocol)
Now In session you just set the session of username and password and you are checking on every page that if this session exists then show the page

Now what if , You have to send a lot of information of this user across multiple requests? In this scenario, You will set all this info in a Stateful session bean
But now a days , In modern frameworks session as well as information , everything is stored in Session beans because From session bean it is easy to manage them.
HTTPSession was used when we were purely on Servlet and somehow JSP technologies

2
  • Just read a article on JAVA EE6 and it says with CDI and the sessions coped annotation a managed bean can be persists across multiple HTTP requests in a web application.So it looks to me that HTTP session functionality can be implemented via a managed bean or HTTP Session
    – jhon.smith
    Jun 21, 2013 at 8:59
  • Yes exactly you can do this.The term you were using Stateful Session Bean for session beans is used in EJB's and the term Managed Bean is used in JSF for session beans.
    – Freak
    Jun 21, 2013 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.