• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Q12(page461) from SCWCD

 
Mohit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not clear with the explanation given for Q12(page 461) in SCWCD kit.
Question is
Consider the following code contained in a file named this.jsp
<jsp:useBean id="address" class="AddressBean" />
<jsp:setProperty name="address" property="*" />
<%@ include file="that.jsp" %>
Which of the following is true about the AddressBean instance declared in the above code
a) The bean instance will not be available in that.jsp.
b) The bean instance may or may not be available in that.jsp, depending on the threading model implemented by that.jsp.
c) The bean instance will be available in that.jsp, and the that.jsp page can print the values of the bean�s properties using <jsp:getProperty />.
d) The bean instance will be available in that.jsp, and the that.jsp page can print the values of the bean�s properties using <jsp:getProperty /> only if that.jsp also contains a <jsp:useBean/> declaration identical to the one in this.jsp and before using <jsp:getProperty/>.
As per my understanding Answer should be option (a). bcoz we have included that.jsp in this.jsp so the bean instance will not be avialable in that.jsp.
But Answer given is (c) and explanation given is
The that.jsp page is included using a directive. Thus, it is a static inclusion, and the two pages form a single translation unit.
I dont understand this. How come we can have the instance of bean avialable in that.jsp file? Scope of bean is page so it wld be avialabe only in this.jsp page and moreover this.jsp has included the that.jsp
so for the translation of this.jsp that.jsp is required
and that.jsp will always get executed independently.
I tried the example and ran that.jsp page i got null pointer exception.
can anybody shed some light on this?
thanks
-MOhit
 
Dee Mike
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohit,
With static inclusion the included file (which can be any text based file) literally becomes a part of the including jsp page. They are actually combined to form a single servlet.
 
Mohit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike
Still i am not clear..
If its true then why did i get null pointer exception when i ran that.jsp.
And second thing in static inclusion, jsp engine writes the content of included file into including file and treat the included file seprately.
Thats why in static inclusion even if you change the content of included file and run the jsp page which has included this file will not reflect the changes.
-MOhit
 
Denny Permana
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohit,
I admit that this code is very bad style for the real life. Of course this case is intended for exam only, not for real life.
The that.jsp will never run separetely well, because it doesn't make the instance of bean (by call <jsp:usebean ... />, so then if we run separately this code by calling <jsp:getProperty ... />, we will get null error (no instance / object reference). I think this question just trying our understanding about static inclusion.
 
Mohit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Confusion Confusion ....
If you see it practical point of view then ans (a) is the correct ans.
But theoritically its something different..
 
Denny Permana
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you that either ans a) or c) is the true choice. But in this case, the context of question is about static inclusion by using "include directive". I think the question is very tricky, so we should choose the best answer regarding the context.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic