Hi, I have made a shopping cart stateful session bean. In one JSP
page, it simply adds some strings to the vector in the cart bean. I
want to be able to click on a link to another page, which can then
retrieve those strings.
I have been trying to read up on doing this, but can't quite figure it
out from what I have read. Below is the code which I use to create an
instance of the Cart. How do I retrieve this instance from another JSP
page. Do I need to call request.getSomethingOrOther, or do I need to
do something with the SessionContext. I am comfused. Any help would be
great.
Cheers,
David
System.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.provider.url", "localhost:1099");
Object reference = null;
try{
InitialContext jndiContext = new InitialContext();
reference = jndiContext.lookup("y0210323/Cart");
}
catch(NamingException e){e.printStackTrace();}
try{
CartRemoteHome home =
(CartRemoteHome)PortableRemoteObject.narrow(reference,
CartRemoteHome.class);
CartRemote shoppingCart = home.create("Duke DeEarl","123");
.........................etc
> Hi, I have made a shopping cart stateful session bean. In one JSP
> page, it simply adds some strings to the vector in the cart bean. I
[quoted text clipped - 7 lines]
> do something with the SessionContext. I am comfused. Any help would be
> great.
You need to pass the EJB reference to the next page, which is typically
achieved by storing it in the (HTTP) session on the first page and then
retrieving it from there on the second. This scales poorly compared
with alternatives, however (see my response to your other question); a
stateful session bean is not really the right tool for the job.
John Bollinger
jobollin@indiana.edu
Dj Frenzy - 30 Nov 2004 12:53 GMT
> You need to pass the EJB reference to the next page, which is typically
> achieved by storing it in the (HTTP) session on the first page and then
[quoted text clipped - 4 lines]
> John Bollinger
> jobollin@indiana.edu
Unfortunately I am doing a degree and a unit requires me to implement
a sales system using EJB's, so I have no choice in the technology I
can use. I know how to pass objects from page to page using the HTTP
session, however I am having difficulty in figuring out how to get the
reference to the instance of my session bean.
From what I can find it seems that I need to call getEJBObject() on
the SessionContext? So in my JSP that wants to retrieve the instance I
call:
CartRemote cart = (CartRemote)
request.getSessionContext().getEJBObject();
Is this right? I can't currently test it because the server has gone
down, so any insight into whether this would be correct would be
great.
Cheers,
David
John C. Bollinger - 30 Nov 2004 13:49 GMT
>>You need to pass the EJB reference to the next page, which is typically
>>achieved by storing it in the (HTTP) session on the first page and then
[quoted text clipped - 10 lines]
> session, however I am having difficulty in figuring out how to get the
> reference to the instance of my session bean.
One of the alternatives I suggested was to use a _stateless_ session
bean or entity bean to access cart information stored in a backend
database (instead of trying to keep the cart information in the
conversational state of a stateful session bean). That would be an
EJB-based solution, would it not?
Furthermore, EJB is completely unsuited to be the primary technology for
any UI, so you must be using other technologies than just EJB anyway.
For a web-based UI you are going to have to use servlets / JSP, and you
are going to want to use session-scope data to thread together a user
session with your application, even if the only such data is EJB
references. The question is how much information you will maintain
there, and how much you maintain on the other side of your EJBs -- not
whether you use servlet-style session-scope data in the first place.
As for the reference, you already have it. It is the value that you get
back from the create() method of your local home interface; you surely
store it in some variable typed as the bean's local interface.
John Bollinger
jobollin@indiana.edu
Dj Frenzy - 30 Nov 2004 12:53 GMT
> You need to pass the EJB reference to the next page, which is typically
> achieved by storing it in the (HTTP) session on the first page and then
[quoted text clipped - 4 lines]
> John Bollinger
> jobollin@indiana.edu
Unfortunately I am doing a degree and a unit requires me to implement
a sales system using EJB's, so I have no choice in the technology I
can use. I know how to pass objects from page to page using the HTTP
session, however I am having difficulty in figuring out how to get the
reference to the instance of my session bean.
From what I can find it seems that I need to call getEJBObject() on
the SessionContext? So in my JSP that wants to retrieve the instance I
call:
CartRemote cart = (CartRemote)
request.getSessionContext().getEJBObject();
Is this right? I can't currently test it because the server has gone
down, so any insight into whether this would be correct would be
great.
Cheers,
David