File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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" />
Ranch Hand

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

Joined: Nov 04, 2001
Posts: 1873
they r just the different ways to obtain ref to the bean object we r having ....
i don c any point in asking "why"...
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
I agree. Here's the link:
subject: use "jsp:getProperty" or "req.getAttribute" to retrieve an object ?
jQuery in Action, 3rd edition