• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jsp and javabean

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
I have tried same code using Forward() also. but same thing.
Please help.
Thanks
Rohini
 
Rohini thacker
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,
I have checked session id in servlet and jsp both are different.
what should I do now?
Rohini
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the jsp you are forwarding to reside in the same application?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
It seems the useBean creates a new session. Pls retrieve the bean using scriplet<% %> and try to access the properties
 
Rohini thacker
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try accessing your bean in request scope within the jsp and use request.setAttribute within the servlet. It should work.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic