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
Thanks and Regards, Ramya.
posted 10 years ago
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.