2 - <jsp:useBean> + BODY - If you utilize <jsp:setProperty> within <jsp:useBean> as body content, The container will utilize "pageContext.findAttribute() for updating the property value"...I mean, it will update the first attribute that it finds.
3- <jsp:getProperty> - don�t have "scope" attribute and the Container finds in all scopes.
On the other hand this works and sets new value for the property (servlet stays the same, we change JSP):
Looks like the <jsp:getProperty> didn't look in all scopes, just page scope, but the <jsp:setProperty> was able to set the new value for the property even if the attribute is in the application scope.
Would be appreciated for any justification, as I can't find the answer by myslef.
But I'd like to point out that there's inconsistency. According to my examples the <jsp:setProperty> is looking in other scopes, and the <jsp:getProperty> is not. They behave differently.
Another thing is, we don't have to use the <jsp:useBean> to make the bean object available for use for the <jsp:getProperty>, we can do it like this and it works.
I've run a quick example on the 1.4 Reference Implementation (Sun AppServer 8), and I think it helps demystify everything.
JSP:
Generated Servlet:
So, as you can clearly see, as Narendra said, <jsp:useBean /> has a default scope of page (confirmed in the spec.) and <jsp:getProperty /> and <jsp:setProperty /> search all scopes for the scoped attribute.
However, you should really always use <jsp:useBean /> along with the other getter/setter actions, just in case your container generates a slightly different structure of JSP and relies on <jsp:useBean /> being invoked first. I believe it is unwise not to use <jsp:useBean />.
Aleksander, I can only suggest there is a bug in your container - what brand/version is it, and have you checked their bug tracker? [ April 20, 2006: Message edited by: Charles Lyons ]
Charles you are always on time I was astonished to find out this behaviour, because spec says <jspProperty> and <jsp:getProperty> will refer to an object through pageContext and it's findAttribute() method. Thank you for clearing all the doubts. We need this for the exam and in fact for our future work.
Something is definately wrong in tomcat. This is because tomcat uses findAttribute and it always return the one in request scope. It just ignore the jsp:useBean.
I tested the code in weblogic 8.1, It returns Session which is what I expected.
your expreiment doesnot prove that something is wrong with tomcat.
even though u used following jsp:usebean
still jsp:getProperty ...
will search for the person bean in all scopes starting from pagescope, and thus will find the bean present in the request. this is not the recomended behaviour as far as jsp 2.1 spec is conscerned, but neither is it disallowed by specs. hence nothing is wrong with tomcat.
also i want to quote a para from jsp 2.1 spec titled 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. 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:set- Property and jsp:getProperty actions in that page unless a jsp:useBean action, or some other action, makes them visible. If the JSP processor can ascertain that there is an alternate way guaranteed to access the same object, it can use that information. For example it may use a scripting variable, but it must guarantee that no intervening code has invalidated the copy held by the scripting variable. The truth is always the value held by the pageContext object.
[ April 22, 2006: Message edited by: Jigar Gosar ]