aspose file tools*
The moose likes JSP and the fly likes Jsp and javabean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Jsp and javabean" Watch "Jsp and javabean" New topic
Author

Jsp and javabean

Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
Hi all,
here are the steps :
. trying to populate javabean in servlet
. put that bean object in session using setAttribute
. pass control from servlet to jsp using
sendredirect
. in JSp using usebean tag I am trying to
access bean using session scopt but in jsp it is not showing me any data
. in servlet it is showing one record but in
jsp 0
I have tried same thing using requestdispatcher also. but same problem. I am trying to execute the code using visual age webshpere test enviroment.
Please help.
Thanks
Rohini
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

maybe you're losing the session between pages??
try printing out the Session ID on both pages -- are they different? if yes -- then make sure your browser accepts cookies. Or better yet... encode all your URLs with response.encodeURL() -- if cookies are disabled, it will automatically append the SessionID to the URL.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
I doubt if the session is getting broken. I think the approach is fine, right up until the redirect. I suspect that a forward() is more appropriate here.
With a redirect, you don't passing control of the request, you create a new request (there is another round trip to the client and back).
But then, I could be wrong


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Hi,
As the session is associated with the request object, if u use sendRedirect the request won't be available so if u try accesing the bean in session scope the values would be null.So use forward to forward the request and get the values from the session object.
Hope this helps
Cheers
Geeta
Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
Hi Everyone,
I have tried same code using Forward() also. but same thing.
Please help.
Thanks
Rohini
Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
hi everyone,
I have checked session id in servlet and jsp both are different.
what should I do now?
Rohini
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Does the jsp you are forwarding to reside in the same application?
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
hi
It seems the useBean creates a new session. Pls retrieve the bean using scriplet<% %> and try to access the properties
Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
Hi All,
here is my code.
I am using Visual Age for java's WTe to test the code.
my jsp is residing under
c:/ibm/VisualAge for Java\ide\project_resources\IBM WebSphere Test Environment\hosts\default_host\default_app\web\jsp\
and my servlet and bean are under shop packages.
I have include this in class path setting of websphere test environment.
is there any problem with my code or do I need to reinstall VAJ.
Please help
Rohini


Pravin Panicker
Ranch Hand

Joined: Oct 05, 2000
Posts: 62
Try accessing your bean in request scope within the jsp and use request.setAttribute within the servlet. It should work.


Pravin R Panicker<br />SCJP,SCWCD
Ani joshi
Greenhorn

Joined: Apr 09, 2003
Posts: 9
Hello Rohini,
In controller, I couldnt see the code where u saved productbean object in the session.
I can see only one line which is session1.setAttribute("ProductBean",str1);
I suppose , u want to save your productbean object in the session.
On which line you performed above task?
Regards
Anirudha
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
Hi
you are putting only str in session not the bean so the bean wont be available in the jsp.
String str1 = "hi";
session1.setAttribute("ProductBean",str1);
insted use
String str1 = "hi";
session1.setAttribute("ProductBean",ProductBean);
that will solve the problm i think
Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
hello everyone,
The actual code was like this
session1.setAttribute("ProductBean",ProductBean);
System.out.println("session1 : "+session1);
RequestDispatcher rd = request.getRequestDispatcher("/jsp/test1.jsp");
rd.forward(request,response);

Sorry for the trouble.'
Regards,
Rohini
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
try to get the bean using <% %> tags in jsp insted of useBean, If it works, the problem is with useBean creating nes session, otherwise try simple res.sendRedirect("jsp-page"). while redirecting dont give http://localhost:8080 (I think it creates the problem insted use relative path /jsp/test1.jsp) & try to get the bean. Otherwise check the session timeout property of the server which u r using
hope this helps
Hugh Jamieson
Greenhorn

Joined: Dec 03, 2002
Posts: 8
A few observations that might help:
Based on your replys, it seems evident that your application is not correctly handling the session. Since you state that the session IDs are different between the servlet & the jsp, this means that the host is tracking these as 2 seperate sessions.
Also, when you getSession() from the request object, you are specifying (TRUE), which means that if the session does not exist, a new one will be created. You might change this to FALSE, and then uncomment the test to check for a NULL session. You may find that a session has not been created.
In addition, the <useBean> tag will create a new instance of a bean if it cannot find one in the scope requested. This makes it pretty difficult to tell how it got created unless you put some signature in the bean to indicate how it was instantiated.
I have run into this issue before with Tomcat & IE. For some reason, the session cookie does not flow back to the server when you initiate the session. I used the debug options of IE to prompt me anytime a cookie was exchanged, and I found that the beginning of the session was NOT sending the start-session cookie when I expected it. I also posted this on the forum, but didn't find anything that really fixed it.
However, I did find something that had more than a causal effect: The hostname. Most of us use http://localhost:8080/... to develop. In this environment, everything worked GREAT. But, as sooon as my URL used a hostname, i.e. http://bigserver:8080/... , this silly session thing got in my face. If I simply replaced the hostname with the hosts IP address, the problem dissapeared!
I have come to the conclusion (and I could be smoking dope) that this is a DNS issue. I know the server is doing host lookups during http traffic, so I think this is plausable.
Anyway, try your code again, but this time, use the IP address. See what happens.
Hugh
Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
Hi All,
As per rgsuresh suggestion, I have tried to get the bean using <% %> tags in jsp insted of useBean, and it worked.
this is my test1.jsp code


Now can any body please explain me why Usebean is not working in my case?
Thanks
Rohini
suresh guru
Ranch Hand

Joined: Sep 07, 2002
Posts: 38
Hi rohini
As of the code which u produced, I am also curious to know why it doesnt worked. But i think u must use the form <useBean/> insted of closing </useBean> and remove session="true" (as the default is true. try and if u find the correct tinng post the results here as i am also wondering the weired behaviour of useBean tag
Rohini thacker
Greenhorn

Joined: Apr 18, 2003
Posts: 8
Hi Everyone,
my problem is got solved now.
I have just come to know one thing
The attribute name that u have used to store oject in session, you have to use same attribute name in JSP:usebean tag to access it.

I will explain this using below code
In servlet
ProductBean ProductBean = new ProductBean();
// populate bean with data
session1.setAttribute("ProductBean",ProductBean);
Now is Jsp u should right following code to access bean.
<jsp:useBean id="ProductBean" class="shop.ProductBean" scope="session"> </jsp:useBean>
That means whatever value u put in id should be same as setattribute.
Regards
Rohini
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jsp and javabean