• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:setProperty without jsp:useBean

 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain the answer for the Mock Question 63 of HFSJ Final Mock Exam?

The question says that "Assuming this is the complete JSP" and uses jsp:setProperty without jsp:useBean action.

This SHOULD raise an error. Right?

I hope answer should be "Lines 4 and 5 will definitely generate errors".

Please explain.
 
Sayak Banerjee
Ranch Hand
Posts: 292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if I am actually answering the question in the book because I have not taken the mock test yet and therefore I don't want to look at that question...but I think I can answer this

The question says that "Assuming this is the complete JSP" and uses jsp:setProperty without jsp:useBean action.

This SHOULD raise an error. Right?


You can use <jsp:setProperty> on its own without using <jsp:useBean>
eg. <jsp:setProperty name="person" property="name" value="Sayak" />

The way it functions doesn't depend on the usage of <jsp:useBean>...in the example above the container will look through all the scopes and set the property on the bean found at the scope to which it's bound....order of searching -page, request, session, application....BUT it will throw an exception if the bean doesn't exist in any of the scopes


Now that we know this.... if we use something like :
<%
foo.Employee p=new foo.Employee();
p.setName("Sayak");
pageContext.setAttribute("person",p);
%>
<jsp:useBean id="person" class="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>
with foo.Employee the same bean class as in HFSJ with "name" & "empID" properties...
Keepin' in mind that the bean "person" exists only in page scope, the result may not be as we normally think it would be....What happens is this:
When <useBean> is executed the container doesn't find the bean "person" in request scope so it creates one and binds it to request scope...but when <setProperty> is executed it starts searching for bean "person" in all the scopes atrting from page scope....it finds one at page scope first and sets the property "name" of that bean(and not the one in request scope)
Therefore
${requestScope.person.name} prints nothing
${pageScope.person.name} prints Fred
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not everyone has HFS&J so please try to paste the full question so that others can better help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic