The mock exam indicated that this would give a "compiler error". Since the jsp action tag was used, there shud be no problem while compiling.I think this shud compile and print the value "SecondValue"?
The id Attribute The id=�name� attribute/value tuple in a jsp:useBean action has special meaning to a JSP container, at page translation time and at client request processing time. In particular: � the name must be unique within the translation unit, and identifies the particular element in which it appears to the JSP container and page. Duplicate id�s found in the same translation unit shall result in a fatal translation error.
which is probably why you get the error.
from the scope section of jsp:useBean
The scope Attribute The scope=�page|request|session|application� attribute/value tuple is associated with, and modifies the behavior of the id attribute described above (it has both translation time and client request processing time semantics). In particular it describes the namespace, the implicit lifecycle of the object reference associated with the name, and the APIs used to access this association. For all scopes, it is illegal to change the instance object so associated, such that its new runtime type is a subset of the type(s) of the object previously so associated.
Even if it worked, you wouldn't have got "secondjsp" because if the container looked for the property on encountering the getProperty, it would first search Page, Request, Session and finally Application.
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
yep you guys are right, it works because its a jsp:include and not a <%@ include
but what baffles me is, that it prints "Second.jsp" and not "FirstValue"
the getProperty uses the PageContext.findAttribute() method to find the attribute, and this method looks for the attributes first in the scope Page -> Request -> Session -> Application therefore it should find the myProperty property in the session scope? am i confused? need to go through again I guess...damn
The answer is compilation error because you don't use fully qualified class name as value for class attribute in <jsp:useBean> standard action. If you used the fully qualified name the output would be firstValue because, as Akshay said, <jsp:getProperty> uses the findAttribute() method to find the attribute.
This code creates a new instance of MyBean in the session scope and sets the value of its property myProperty to "FirstValue". Then It includes the Second.jsp page. This means whatever would be the output of second.jsp would be included in the output of First.jsp. Whenever we include some page, We also send all the objects in the current session scope to the included page.
Here this is a differnt translation unit hence there is no name conflict. But since this page has got the object of MyBean in session from First.jsp, it does not creates a new Instance and just modifies the value of myProperty to SecondValue.
When we come back to First.jsp and use getProperty, remember that Second.jsp has changed the value of this object which is in session. Hence we get the output as SecondValue instead of FirstValue.
Hope this Helps!!
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?