Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object with page scope

 
Jerson Chua
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are object of page scope accessible to the included JSP page?
thanks.
[ June 05, 2002: Message edited by: Jerson Chua ]
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jerson Chua,
This is really a tricky question, but a good question!
The anwser is:
YES, if you use include directive;
NO, if you use jsp:include.
example:
in a Java class, you have:
==========================
package anypackage;
public class MyBean {
private message = null;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
in test1.jsp, you have the code:
================================
<jsp:useBean
id="myBean"
class="anypackage.MyBean"
scope="page"/>
<jsp:setProperty
name="myBean"
property="message"
value="HelloWorld"/>
<%@include file="/test2.jsp"%>
in test2.jsp, you have the code:
================================
<jsp:getProperty
name="myBean"
property="message"/>
if you call test.jsp, you will have no problem and get an output of "HelloWorld". Because the test2.jsp was included at the page-translation time, so the jsp implimentation object has already combined these two jsp pages. You are actually calling one servlet.
Now, if you change the include directive in test.jsp to:
<jsp:include page="/test2.jsp"/>
Now call test.jsp, you will get an error, something like: "Attempted a bean operation on a null object".
In this case, the test2.jsp was included at the request time, and you are actually calling two jsp implemetation objects, while the second (included) implementation object can not see the object defined in the first implementation object.

Hope this helps.
 
Jerson Chua
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
Just curious Tony, have you passed the exam? if not, how long have you been preparing for the exam?
 
Tong Chen
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure, Jerson.
Actually I am ready to take the exam and will do it very soon, maybe next week.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic