Attributes in Tag Files: 'Tag' scope or Page scope?

Ted Young

Greenhorn

Posts: 4

posted 9 years ago

I'm making my way through the Head First Servlets and JSP book and I've come across something which has got me confused.

In the mock exam for Chapter 10, Custom Tag Development, Page 567, option F for Q. 18 says that "for every attribute declared and specified in a Tag File, the container creates a page-scoped attribute with the same name."

The two statements seem to contradict each other. So which is right? Or am I missing something here? [ September 20, 2007: Message edited by: Gnidocta Doogon ]

Michael Ku

Ranch Hand

Posts: 510

posted 9 years ago

I do not have the book handy, but are they talking about tag files in one case and custom tags in the other?

Ted Young

Greenhorn

Posts: 4

posted 9 years ago

Originally posted by Michael Ku: I do not have the book handy, but are they talking about tag files in one case and custom tags in the other?

No, actually. The alert I mentioned was in the section for tag files.

So, which is right, in any case? Attributes declared with an <%@ attribute %> directive would be in the tag's scopre only, right? Somewhat like a code block's local variable?

Michael Ku

Ranch Hand

Posts: 510

posted 9 years ago

I think that is correct. When I get home I will check to see if you got a good answer. If not, then I will check what the book says and see if I can answer your question.

Ted Young

Greenhorn

Posts: 4

posted 9 years ago

Originally posted by Michael Ku: I think that is correct. When I get home I will check to see if you got a good answer. If not, then I will check what the book says and see if I can answer your question.

Thank you

Michael Ku

Ranch Hand

Posts: 510

posted 9 years ago

I think that the key phrase is "attribute declared and specified in a Tag File.

The Bang Box on 494 shows that the attribute in the tag (which is on a page that is not the tag file itself) cannot be used anywhere on the page that is using the tag. The tag file itself is a different matter. In it the attribute that was declared can be used anywhere in this tag file.

When you are in the page that is using the Tag File, you can only assign a value to the attribute that was declared in the Tag File itself. This value can and probably will be used in the Tag File itself

So you have a page that is using a Tag File. The page using the Tag File will assign a value to the attribute that was declared in the Tag File itself. The Tag File will use this value.

Make any sense?

Ted Young

Greenhorn

Posts: 4

posted 9 years ago

Ah, I think I get it now. This is what I figure:

A Tag File is in essence a JSP that is used sorta like an include. So the attribute is in the Page scope of the Tag File ( since it is a JSP in the end ) but that attribute doesn't overflow into the scope of the JSP that is actually using the tag.