Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use "jsp:getProperty" or "req.getAttribute" to retrieve an object ?

 
Ian Yang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a servlet code, it does --
req.setAttribute("MyObj", o);
then it does a
forward(req, res);
to forward it to "My.jsp".
Question is: in "My.jsp", are the following three totally euivalent ?
1. <% Object o = req.getAttribute("MyObj"); %>
result = <%= o.getSomthing() %>
2.<jsp:useBean id="MyObj" class=".." scope="request />
result = <%= MyObj.getSomething() %>
3.<jsp:useBean id="MyObj" class=".." scope="request />
result = <jsp:getProperty name-="MyObj" property="somthing" />
Thanks,
Ian
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the three parts of code are equivalent.
Why?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they r just the different ways to obtain ref to the bean object we r having ....
i don c any point in asking "why"...
regards
maulin.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the three parts of code are equivalent.
ummmmm....we need to use a little caution.
They will be equivalent ONLY when a object called
"MyObj" already exists in the request scope.
If not the first option will return null. The other two options will create a new object with
a ref of MyObj and return (since the "class" atttribute of the useBean tag was used).
Just clarifying....
- satya
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic