Forums Register Login

Page-scope vs tag-scope

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
    +Pie Number of slices to send: Send
    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!
    +Pie Number of slices to send: Send
    You should use a variable directive to provide a variable into calling page scope.
    The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1873 times.
    Similar Threads
    Attributes in Tag Files: 'Tag' scope or Page scope?
    JSTL vs. javabeans
    doubt on two questions..(Topic:: Session Migration & EL)
    HFS&JSP confusion...
    HFS page 567, question 18
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 07:58:40.