here I have another very nice one .
Given a JSP page:
The tag handler for my:tag1 is Tag1Handler and extends TagSupport. The tag handler for my:tag2 is Tag2Handler and extends SimpleTagSupport. The tag handler for my:tag3 is Tag3Handler and extends TagSupport.
Which is true? (Choose all that apply.)
Tag3Handler CANNOT access the instance of Tag1Handler because Simple tags do NOT support access to the tag parent.
Only Classic tags are considered in composing parent/child tag hierarchy; therefore, Tag3Handler may use the getParent method only once to gain access to the instance of Tag1Handler.
Tag3Handler may use the getParent method twice to gain access to the instance of Tag1Handler.
Only BodyTag handlers can access the parent/child tag hierarchy; therefore, Tag3Handler CANNOT gain access to the instance of Tag1Handler.
Any answer with an explanation is welcome but please with your own explanation and please don't give away the information where I took this question from .
[ September 10, 2005: Message edited by: Darya Akbari ]
thanks for your response. To my opinion Answer C is impossible because the parent of tag3 must be of type Tag. The parent of tag3 is tag2 which is of type JspTag.
Look how Tag and JspTag are related:
We can say now the following:
Do you agree :roll:
tag3 is a Classic Tag (implementing the Tag interface) and it's getParent method returns a Tag object.
The parent of tag3 is tag2 isn't it ? Hence tag2 should be of the same type (Tag) but it is not. tag2 is a Simple Tag and of type JspTag
That does not fit together .
C is the right answer. If you read the chapter carefully, it says that even classic tags can have simple parents. When you call getParent() on the classic tag, it would return a Tag type, which I agree cannot refer to a SimpleTagSupport class. But you can do a work-around for this using the TagAdapter class. So, C is right. (Tag Adapter is discussed in a footnote in the chapter). But I think we will not get such questions in the exam as TagAdapter is not covered
which chapter are you talking about . Did I say where I took my thoughts from .
Nevertheless, I believe you are talking about page 540 from the HFSJ book and you are right that my thoughts are based on exactly the description there.
But let's share the information given there for others who don't have that book:
from HFSJ p. 540
Simple tags can have Classic parents
This is not a problem, because a SimpleTag's getParent() returns type JspTag, and Classic tags and Simple tags now share the JspTag super interface. Actually, Classic tags can have Simple parents, but it takes a slight hack to make that work because you can't cast a SimpleTag to the Tag return value of the Tag interface getParent(). We won't go into how to access a Simple tag parent from a Classic child tag*, but all you need to know for the exam is that by using getParent(), a Classic tag can access Classic tag parents, and a Simple tag can access either a Classic or Simple parent.
*If you are really curious, look at the TagAdapter class in the J2EE 1.4 API
Look to the bold text. That speaks clear against Answer C . I don't know how the trick with TagAdapter works but with getParent() it does not work.
from Head First Servlets and JSP errata list p. 550:
 question 3;
Option C is invalid.
Unfortunately, that leaves us with a mock question that doesn't have a valid answer.