cancel
Showing results for 
Search instead for 
Did you mean: 

Stateful session bean

Former Member
0 Kudos

I am creating a web application using JSPs and want to keep stateful information from one call to the next. In the first jsp I run the code:

-


InitialContext ic = new InitialContext();

StatefulTestLocal testbean = ((StatefulTestLocalHome)ic.lookup("java:comp/env/ejb/StatefulTestBean")).create();

testbean.setUsername(request.getParameter("uname"));

-


In the second JSP I run the code:

-


InitialContext ic = new InitialContext();

StatefulTestLocal testbean = ((StatefulTestLocalHome)ic.lookup("java:comp/env/ejb/StatefulTestBean")).create();

testbean.setUsername(request.getParameter("uname"));

out.println("<hr>Username get<br>");

out.println(testbean.getUsername());

out.println("<hr>");

-


The second JSP always prints null for the username. I always thought when creating a stateful session bean, it looks for an instantiated class that is tied to the client and then if it isn't found, creates a new class. It appears to always be creating a new object.

Every example I see, shows then creating the stateful and then stuffing it into the session. Is this really the way to do it?

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

I am creating a web application using JSPs and want to keep stateful information from one call to the next. In the first jsp I run the code:

-


InitialContext ic = new InitialContext();

StatefulTestLocal testbean = ((StatefulTestLocalHome)ic.lookup("java:comp/env/ejb/StatefulTestBean")).create();

testbean.setUsername(request.getParameter("uname"));

-


In the second JSP I run the code:

-


InitialContext ic = new InitialContext();

StatefulTestLocal testbean = ((StatefulTestLocalHome)ic.lookup("java:comp/env/ejb/StatefulTestBean")).create();

testbean.setUsername(request.getParameter("uname"));

out.println("<hr>Username get");

out.println(testbean.getUsername());

out.println("<hr>");

-


The second JSP always prints null for the username. I always thought when creating a stateful session bean, it looks for an instantiated class that is tied to the client and then if it isn't found, creates a new class. It appears to always be creating a new object.

Every example I see, shows then creating the stateful and then stuffing it into the session. Is this really the way to do it?

Former Member
0 Kudos

Still Looking for an answer. Anybody?

Vlado
Advisor
Advisor
0 Kudos

Hi Nathan,

A stateful session is started (stateful session bean instance created) when the client calls one of the create methods on the (local) home interface. Any subsequent client call on the returned component interface proxy object is then redirected to that same bean instance. If you call create() twice then you'll have two different stateful sessions (although maybe only one http session - as in the case above).

A very nice and short summary on stateful vs. stateless beans:

http://help.sap.com/saphelp_nw04/helpdata/en/41/05503e30a9d549e10000000a114084/frameset.htm

HTH!

-Vladimir

Former Member
0 Kudos

My thought was that you did not have to carry the stateful session bean within the session.setAttribute. That if you did a lookup for it, that it would find the already instantiated object that was tied to the session.

If we are in a multi-server cluster, does the session of the JSP get replicated across all servers?

Vlado
Advisor
Advisor
0 Kudos

> My thought was that you did not have to carry the

> stateful session bean within the

> session.setAttribute. That if you did a lookup for

> it, that it would find the already instantiated

> object that was tied to the session.

Well, this is not how it works. The lookup (at least in EJB versions <= 2.1) returns the EJB home object, that is the home of all instances of that particular bean type. And every create() method triggers the creation of a new stateful session bean instance.

> Every example I see, shows then creating

> the stateful and then stuffing it into the session.

> Is this really the way to do it?

Yes, I think so.

> If we are in a multi-server cluster, does the session

> of the JSP get replicated across all servers?

Check <a href="http://help.sap.com/saphelp_nw04/helpdata/en/90/044cc585eaba42b649f161818b0fdf/frameset.htm">HTTP Sessions and Failover of Web Applications</a> as well as <a href="http://help.sap.com/saphelp_nw04/helpdata/en/8f/d6e45953a494499ea1b79ab16321d2/frameset.htm">Failover for Enterprise Beans</a>.