Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Simple Tag & Classic Tag handler mock exam question - what's YOUR answer?

 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.)

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 .

Regards,
Darya
[ September 10, 2005: Message edited by: Darya Akbari ]
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose C is right answer.
I think both Simple and classic tags have method getParent or findAncestorWithClass
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

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:

  • Tag IS A JspTag
  • JspTag IS A JspTag
  • JspTag IS NOT A Tag


  • Do you agree :roll:

    Regards,
    Darya
     
    David Ulicny
    Ranch Hand
    Posts: 724
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why must be parent of tag3 of type Tag? If Tag is a JspTag?
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi David,

    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 .

    Regards,
    Darya
     
    David Ulicny
    Ranch Hand
    Posts: 724
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What is the right answer?
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would say that there is no right answer

    Regards,
    Darya
     
    B.Sathish
    Ranch Hand
    Posts: 372
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Regards,
    Darya
     
    Darya Akbari
    Ranch Hand
    Posts: 1855
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look what HFSJ's errata list says :


    from Head First Servlets and JSP errata list p. 550:

    [550] question 3;
    Option C is invalid.
    Unfortunately, that leaves us with a mock question that doesn't have a valid answer.




    Regards,
    Darya
     
    B.Sathish
    Ranch Hand
    Posts: 372
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have seen the errata, but I doubt whether it is right. I thought we could use TagAdapter to do something to the return type of getParent() and somehow get a reference to the simple tag handler
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic