• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Page-scope vs tag-scope

 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm reading "Head First Servlets and JSP" and have a question concerning page-scope vs tag-scope. I'm having trouble with one of the Mock Exam questions:

Chapter 10 Mock Exam, answer to question 18, p.599:
"Which are true about the Tag File model?"

One of the answers is:
"For each attribute declared and specified in a Tag File, the container creates a page-scoped attribute with the same name."

Shouldn't it read "creates a tag-scoped attribute"? A page-scoped attribute would be visible outside of the tag, wouldn't it? For example:

The JSP:

The generated page:

This isn't what really happens--what happens is it can't find ${foo}, so it prints an empty string. But isn't this what should happen if tag attributes are page-scoped? Thanks!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is called page scope (what is tag scope ?), but the context is different. It's not the same page context as the page calling the tag. That's why you don't see the value in your JSP. Check the JSP specification, JSP.8.3 Semantics of Tag Files :

  • The tag file implementation must create and maintain a second instance of JspContext called a JSP Context Wrapper. If the Invoking JSP Context is an instance of PageContext, the JSP Context Wrapper must also be an instance of PageContext. This wrapper must be returned when getJspContext() is called.
  • For each attribute declared and specified, a page-scoped variable must be created in the page scope of the JSP Context Wrapper.


  • Which doesn't mean that I agree with HFSJ's answer And it looks like it's already in the unconfirmed errata, [567] Question 18, Option F.
     
    Michael Angstadt
    Ranch Hand
    Posts: 277
    Eclipse IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah ok, so a tag file creates its own page-scope. In that case, it looks like the answer is correct! The book mentions "tag-scope" in a couple places, but it also says that it isn't a "real" scope...that just confused me I guess. Thanks for your help!
     
    Lucas Smith
    Ranch Hand
    Posts: 808
    1
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You should use a variable directive to provide a variable into calling page scope.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic