• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

jsp:useBean

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone help me in understanding the scope attribute of jsp:useBean tag. If I set the scope to session, would the bean property be accesible using jsp:getProperty tag from any jsp file? Also shd I have the jsp:useBean re-declared in the pages that I would be accesing the property? This is my understanding, but when I tried out with a code, I get the value as null.

Code where I am setting the bean values.


Code 1 - where I access it. It works fine here.



Code 2 - Here I am trying to acess the session bean and it does not work.
I get the value as null


Pls help.

Thanks and Regards,
Ramya.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your servlet, you are storing the Person in the Request object not the Session object:



Also, in your first JSP, you are creating a bean, storing it in the session and populating it from Person in the request object. So, yes, you will see the value when you call getProperty.

In your second JSP, you are creating a bean, storing it in the session but never populating it. (There is no setProperty tag.) So, no, you will not see the value when you call getProperty.

The above assumes that you start a new session then call the servlet which forwards to one of the JSPs. (The useBean tag will check the Session for an existing Person and use that if it exists, but it won't in a new session.)

Suppose you change the scenario such that the servlet executes and forwards to JSP 1 and then you call JSP 2 in the same session. In this case, you would see the name displayed in JSP 2 because JSP1 would have stored a populated Person object in the Session.
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic