aspose file tools*
The moose likes JSP and the fly likes use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "use "jsp:getProperty" or "req.getAttribute" to retrieve an object ?" Watch "use "jsp:getProperty" or "req.getAttribute" to retrieve an object ?" New topic
Author

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

Ian Yang
Ranch Hand

Joined: Nov 27, 2001
Posts: 44
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

Joined: Nov 22, 2008
Posts: 18944
Yes the three parts of code are equivalent.
Why?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use "jsp:getProperty" or "req.getAttribute" to retrieve an object ?
 
Similar Threads
<jsp:useBean>
running JSPs during application server startup
How to retrieve a Vector of beans without using taglib ?
reading results from javabean into JSP
Bean standard action doubt