Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can an attribute set in request scope be found by JSP standard actions in session scope?

 
Tej Ernesto
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a jsp which is displaying a bean property called name and the bean is PersonVO

=========
PersonVO
=========


I am displaying this with the following code


by hitting the servlet PersonServlet



but what i dont understand is i set the personvo object in request scope in servlet and iam trying to find the personvo object in session scope using jsp standard action but it was able to find it

the code in person_jsp.java is


Even in above code it is trying to get the attribute personvo from session scope but i set the personvo object in request scope in my servlet

Can some on explain me why?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot set something in request scope, and expect it to be available in another scope.

If you put your socks in the top drawer, would you be surprised to find that they are not also in the bottom drawer? Of course not! Why would you expect your socks to be available in session scope if you put them in request scope?
 
Tej Ernesto
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear

As you can see i put personvo attribute in request scope in PersonServlet



and accessin in JSP using jsp standard action tag with session scope..



how is it possible even in the translated jsp to servlet it is finding the attribute in session scope and it was able to get it how is it possible



My question is how an attribute set in the request scope was found when searched using the session scope??
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you want to invoke the jsp:useBean? I think you don't need that as you are already setting the request attribute in the servlet, you can just access it in your jsp directly:

 
Tej Ernesto
Greenhorn
Posts: 19
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i can do that..

My question is i set the attribute in request scope and im trying to find that attribute in session scope in jsp using jsp:usebean.

and it could able to find it i would like to know how it was able find an request scoped attribute in the session scope..
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using the useBean you are creating a new instance of PersonVO and setting it in the Session scope, that might be the reason why your generated Servlet is trying to access it. I am not an expert in JSPs but I think if you change the scope to request you might get to use the one you set in your Servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is plain as day: you can't. If you place the scoped variable into request scope, you need to look for it in request scope. It's not in session scope, so you can look there as hard as you like, but it's not there. Never will be.

If you put your dinner down on the kitchen table, would you expect it to magically appear in the bedroom? No, you put it in the kitchen, so you need to go back to the kitchen to get it.

If you put the variable into request scope, you need to get it from request scope. Simple.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of the different scope as different hashmaps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!