This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.)
A. Tag3Handler CANNOT access the instance of Tag1Handler because Simple tags do NOT support access to the tag parent.
B. 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.
C. Tag3Handler may use the getParent method twice to gain access to the instance of Tag1Handler.
D. 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 .
Darya, 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
Joined: Aug 21, 2004
Hi B Sathish,
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.