Dave Ford wrote:
>
> >From within a body tag, there seems to be multiple writers for outputting
> data. What is the difference between these 3 choices:
>
> bodyContent.getEnclosingWriter().println("Hello");
> bodyContent.println("Hello");
> pageContext.getOut().println("Hello");

The BodyContent class is only used when you develop your own custom
tag handlers. An instance of this class is used to capture the
evaluation of the elements body so the tag handler (if it's a BodyTag)
can get access to it and process it.

  BodyContent.getEnclosingWriter()
    Returns the writer for the parent element, or the main JspWriter
    for the page if the element is not nested within another JSP
    action element. You use this method to get a Writer to use for
    the output your tag handler generates. If your tag handler extends
    BodyTagSupport, you can use getPreviousOut() as well; it has the
    same result.

  BodyContent.println()
    This method is inherited from the Writer superclass (along with a lot
    of other similar methods). It writes to the BodyContent instance itself
    and is intended to be used by the generated code to capture the
    content generated by the element's body so that the tag handler can
    process it later. You're not supposed to use this method in your own
    code.

  PageContext.getOut()
    This method returns the currently "active" JspWriter or BodyContent
    instance. The code generated for the JSP page reassigns "out" to a
    BodyContent instance when it encounters a custom action with a tag
    handler that implements BodyTag. This method therefore returns the
    main JspWriter if it's called from a scriptlet on the "top level"
    in a JSP page and a BodyContent instance if it's called from a scriptlet
    within the body of a BodyTag custom action. It's intended to be used
    by the generated code, and it can also be used by scriptlets that
    want to call println() etc. In general though, I recommend that you
    do not use scriptlets to generate output; let the JSP container
    handle that and use scriptlets only for simple conditional statements:

      <% if (someCondition) { %>
         This text is added to the response by the container if the
         condition is true.
      <% } else { %>
         This is added if it's false.
      <% } %>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to