File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Extending BodyTagSupport (HFS CH10 Q9) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Extending BodyTagSupport (HFS CH10 Q9)" Watch "Extending BodyTagSupport (HFS CH10 Q9)" New topic
Author

Extending BodyTagSupport (HFS CH10 Q9)

Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
Can someone explain why answer 9C (chapter 10) is not true (p594). The provided 'reason' is that returning EVAL_BODY_BUFFERED directs the body to a buffer which "this" tag does not process. However, the state diagram on p563 reveals the body is evaluated after doInitBody(). Furthermore, page 2-72 of the 2.1 spec states, "[BodyContent] will be passed to doInitBody(); then the body of the tag will be evaluated".

This is an area that was glossed over in the book. However, from the information provided I assume you override either setBodyCOntent or doInitBody if manipulation of the BodyContent is required. And if you fail to override either of those methods, returning EVAL_BODY_BUFFERED produces the results that would have been obtained if EVAL_BODY_INCLUDE had been returned. If this is correct, 9C (return EVAL_BODY_BUFFERED) would also be true.
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
I'll just write the code, which is what I have been doing for many of the issues I have turned up. As a result, no response necessary.
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
As indicated in my original post, when extending BodyTagSupport, if setBodyContent and doInitBody are not overridden, returning EVAL_BODY_INCLUDE produces the same rendered result as returning EVAL_BODY_BUFFERED. As a result, chapter 10 Q9 (pg 594) should have B and C as correct answers. Code provided below:

testJSP.jsp:
<%@ taglib prefix="mytags" uri="BodyTagTesting" %>
<html><body>

<mytag:tagTest>BodyContent</mytag:tagTest>

</body></html>

testJSP2.jsp:
<%@ taglib prefix="mytags" uri="BodyTagTesting" %>
<html><body>

<mytag:tagTest2>BodyContent2</mytag:tagTest2>

</body></html>

WEB-INF/BufTag.java:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;

public class BufTag extends BodyTagSupport {
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
}

WEB-INF/BufTag2.java:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;

public class BufTag2 extends BodyTagSupport {
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}
}

WEB-INF/bufTest.tld:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising BodyTagSupport functionality.</description>
<tlib-version>1.0</tlib-version>
<short-name>BodyTagTesting</short-name>
<uri>BodyTagTesting</uri>

<tag>
<name>tagTest</name>
<tag-class>BufTag</tag-class>
<body-content>tagdependent</body-content>
</tag>
<tag>
<name>tagTest2</name>
<tag-class>BufTag2</tag-class>
<body-content>tagdependent</body-content>
</tag>
</taglib>
Pham Hoai Van
Greenhorn

Joined: May 20, 2008
Posts: 15
i didn't get what you have. how comes?
here is my code:

and jsp:

when doStartTag return EVAL_BODY_INCLUDE i got the body content, but when return EVAL_BODY_BUFFERED i got nothing. exactly as HF's answer.
Further more, in case returning EVAL_BODY_BUFFERED where did the body content goes? i tried to log the bodyContent out but nothing there.

Any idea?
gopal venu
Ranch Hand

Joined: Jan 06, 2006
Posts: 55
for getting the body two ways

1.BodyTagSupport.getPriviousOut()
2.PageContext.popBody();

Try it..make sure use them in proper life cycle method..doEndTag() is fine one for both cases...

Let me correct if i m wrong
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
As I said in my original post, "if you fail to override either of those methods, returning EVAL_BODY_BUFFERED produces the results that would have been obtained if EVAL_BODY_INCLUDE had been returned." I could be wrong. However, since I provided the code that met the above requirement, to confirm or refute my stated results it would have been simpler just to run my code. You have provided different code and you indicate it produces different results, which does not address the issue completely.
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
I will just add that as indicated in my original post, info in this area is lacking. If I did override the methods in question, I would also consider invoking the superclass methods (even though "no action" is indicated for doInitBody()). However, I was considering functionality that is present when the methods in question were not implemented, which I believe is addressed in the code I provided.
Dee Brown
Ranch Hand

Joined: Jun 14, 2008
Posts: 94
Also, see the following post for an independent assessment of "return EVAL_BODY_BUFFERED" results:

Doubt Enthuware Mock Exam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending BodyTagSupport (HFS CH10 Q9)