This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Apparently, the preceding answers seem wrong. Here is what says the JSP 2.0 specification:
The jsp:useBean action is quite flexible; its exact semantics depends on the attributes given. The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes.
And in the Tomcat 5.0.30 source code (org/apache/jasper/compiler/Generator.java), I found that Tomcat uses the JspContext.getAttribute(String name, int scope) and not the JspContext.findAttribute(String name). And if the scope attribute is not given by the developper, the "page" scope is used by default.
So the scope tag attribute is well used when retrieving the bean.
Rohit, maybe you should check an error somewhere else: - Check if the name attribute of foo.mybean is not declared as static - Check that you have no typo in your real code (the one you pasted in your question is right) - Check that your server isn't using another version of your code (bad web-app location, outdated cache, ...)
Apart from this, I cannot imagine what has gone wrong.
I hope these informations will help you.
Joined: Nov 28, 2004
To be precise, Pushkar, the jsp:useBean action doesn't use findAttribute (I think) to find the bean. The JSP 2.0 spec says: "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."
So the jsp:useBean action locates correctly the bean in the session scope (mb). But, Francois, the problem of Rohit arises when he uses the jsp:getProperty action, which uses findAttribute and so, it starts looking for the attribute in the order page, request, session and application. And, therefore, if finds first "the other" bean (mb1).
My conclusion is the following: if there's another attribute with the same name and a more specific scope, it is not possible to get the attribute with the less specific scope using jsp:getProperty.
Jose, you are right: the getProperty indeed uses the findAttribute method. This seems to be a bug of the specification that will effectively prevent someone to get a "larger" scope attribute if a "narrower" one exists.
And there is another thing odd in the code about this tag: it uses the bean type of the bean declared with the useBean tag.
Here is an example:
Imagine you store a bean of a class foo.Person with a "firstName" property under session scope with name "foo". You then store a foo.Address bean with only a "city" property under request scope with name "foo".
There must be a reason for the jsp:getProperty action to have this odd behavior: "If there's another attribute with the same name and a more specific scope, it is not possible to get the attribute with the less specific scope using jsp:getProperty."
Why does it use the findAttribute method to find the bean? Why doesn't it have a "scope" attribute?
Any explanation would or supposition will be appreciated.
Joined: Jan 11, 2005
Is there n e way we can get correct explanation for this, instead of making some wild guesses?
Can we report it to Sun so that we will get exact answer for this