File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt regarding useBean and getAttribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt regarding useBean and getAttribute" Watch "Doubt regarding useBean and getAttribute" New topic

Doubt regarding useBean and getAttribute

Nitin Kumar

Joined: Apr 18, 2005
Posts: 29

I have this servlet :

and this JSp : result.jsp

Person is a simle Java Bean having property "name".

When I access the Servlet, the output is

Person from getProperty: TestUser
Person from session:

I am not able to understand why "TestUser" is not shown in second line, since a session scoped attribute is successfully set, as verified by first line,

it works fine and TestUser is shown in both lines in these two cases

a) If I comment out the line 1) in servlet
b) Reverse the attribute scopes in Servlet and Jsp, i.e uncomment the lines 2) and 3) in Servlet and use scope = "request" in jsp

Please help me understanding the logic behind these observations

Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

That's an example of the kind of problems which may arrise when you put attribute with the same name in different scopes. The getProperty/setProperty actions look for "person" in one of the scopes, using findAttribute, and finds it in the request scope. So you're actually setting TestUser into the person in request scope.

JSP.5.3 <jsp:getProperty>

The value of the name attribute in jsp:setProperty and jsp:getProperty will refer to an object that is obtained from the pageContext object through its findAttribute method.

However, the spec continues :

The object named by the name must have been "introduced" to the JSP processor using either the jsp:useBean action or a custom action with an associated VariableInfo entry for this name. If the object was not introduced in this manner, the container implementation is recommended (but not required) to raise a translation error, since the page implementation is in violation of the specification.
Note : A consequence of the previous paragraph is that objects that are stored in, say, the session by a front component are not automatically visible to jsp:setProperty and jsp:getProperty actions in that page unless a jsp:useBean action, or some other action, makes them visible.

I would expect the container to either skip the person in the request scope, or to raise an error, but as the spec says, the container implementation is recommended (but not required) to raise a translation error. Your container chooses to use the attribute that was set by the servlet, even if it's not been introduced by the jsp:useBean action.

[My Blog]
All roads lead to JavaRanch
Nitin Kumar

Joined: Apr 18, 2005
Posts: 29
Hi Christophe,

Thank you for the explaination. It means we should try to avoid using same attributes in different scopes.

I agree. Here's the link:
subject: Doubt regarding useBean and getAttribute
It's not a secret anymore!