This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ Final Mock Question 12. P.767

 
Annie McCall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What answer would you give to this question and please explain.

Public class WorthlessTag extends TagSupport {

private String x;
public void setX(String x) { this.x = x; }
public int doStartTag() throws JspException {

try{ pageContext.getOut().print(x);}
catch (IOException e) {}
if ("x".equals(x))
return SKIP_BODY;
else
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {

try{ pageContext.getOut().print("E");}
catch (IOException e) {}
if ("y".equals(x))
return SKIP_PAGE;
else
return EVAL_PAGE;
}

}

and the given TLD excerpt:
<tag>
<name>worthless</name>
<tag-class>com.mycom.WorthlessTag</tag-class>
<attribute>
<name>x</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


and given this complete JSP page:

<%@ taglib uri="somevaliduri" prefix="w" %>
<w:worthless x="x" />
<w:worthless x="<%=Boolean.True.toString()%>" />
<w:worthless x="y" />
<w:worthless x="z" />

What output does the JSP generate?

A. xE
B. x trueE yE
C. xE trueE yE
D. xE trueE yE zE
E. x <%=Boolean.True.toString()%>E yE
F. xE <%=Boolean.True.toString()%>E yE
G. xE <%=Boolean.True.toString()%>E yE zE

Remember tag's body-content is equal to empty!

[ September 23, 2005: Message edited by: Annie McCall ]
[ September 23, 2005: Message edited by: Annie McCall ]
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Annie,

please allow me to put the question again here because you missed the indents which makes it hard to read.




The right answer must be C. Why do you see the empty <body-content> as a problem. Don't all tags have an empty body?

So we only need to look at the doStartTag and doEndTag methods. You can begin with line 2 til line 4 where we have y. And when you look at y in the doEndTag method you can see the SKIP_PAGE which means that the rest of your JSP will no be processed.

Regards,
Darya
 
Annie McCall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darya,
The reason I think <body-content>empty</body-content> may be a problem is because when we hit statement <w:worthless x="<@=Boolean TRUE.toString()%>" />
will cause the doTag to perform
if ("x".equals(x))
return SKIP_BODY;
else
return EVAL_BODY_INCLUDE;


since x is not equal to "x", the doStartTag will return EVAL_BODY_INCLUDE.

Since the tag is defined as empty, and according to the HFSJ on page 534, it says that if the TLD declares an empty body, doStartTag() MUST return SKIP_BODY. I am assuming maybe a runtime error will occur. I am not sure since I haven't tested it.

Not sure, that's why I thought we couldn't return a EVAL_BODY_INCLUDE if tag is defined as empty.

Let me know what you think.

[ September 23, 2005: Message edited by: Annie McCall ]
[ September 23, 2005: Message edited by: Annie McCall ]
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Annie,

I looked at HFSJ p. 534 and my first impression was that the statements on this page are all dedicated to BodyTag and since you extend from TagSupport in your example and NOT from BodyTagSupport I still believe that C is valid.

Nevertheless a test would solve that issue, so what are you waiting for

Let us know the result.

Regards,
Darya
 
Annie McCall
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darya,
ok. I tested it and I got xE trueE yE which is C.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Regards,
Darya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic