tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ward, Jeff" <jw...@BenefitPoint.com>
Subject RE: Using a jsp:include action (JSP 1.1) within a BodyTag taglib tag
Date Tue, 30 Jan 2001 21:18:35 GMT
  Sure, the code is simple. It follows the spec(tag 1.0/jsp1.1) and I have
used it on WL 51 and Tomcat 3.2.1. Sorry if it doesn't compile
(cut/paste/make it pretty :) . There are other variations on the theme (like
providing your own servlet base class) but his one is simple and doesn't
intrude on the container's ability to optimize. The one big downside is that
this buffers everything so nothing will get back to the client until it is
all rendered. Also, if this page includes anything or if ANYTHING writes
directly to the response you will have problems.
  Hope this helps.
  -Jeff Ward


The code to redirect is:
  
/**this method will set up the diverted out to point to the current out of
the
    * passed in pageContext and include the passed in page
    */
  public static void include(PageContext pageContext, String page) throws
JspException
  {
    try
    {
      //set the diverted out
      pageContext.getRequest().setAttribute("divertedOut",
pageContext.getOut());
      //include the page
      pageContext.include(page);
      //remove the out
      pageContext.getRequest().removeAttribute("divertedOut");
    }catch(Exception e){
      throw new JspException("Include tag: Unknown exception:" +
e.getMessage());
    }
  }

And the included page should look like this:

/**this class allows for includijng pages within nested tags by writing it's
  *body content to an alternate out, the out passed in the request.
  */
public class IncludedPageTag extends BodyTagSupport
{

  JspWriter divertedOut = null;
  
  //make sure the out doesn't stick around
  public void release()
  {
    super.release();
    divertedOut = null;
  }
  
  public int doStartTag() throws JspException
  { 
    //nothing to do here but allow the body to process
    return EVAL_BODY_TAG;
  }
  
  public int doAfterBody() throws JspException
  {
    BodyContent body = getBodyContent();
    //find the diverted out if it exists
    divertedOut =
(JspWriter)pageContext.getRequest().getAttribute("divertedOut");
    try
    {
      if (divertedOut != null)
      {
        //if we found the out print to it
        body.writeOut(divertedOut);
      }else{
        //guess we didn't find the out, just display
        body.writeOut(getPreviousOut());
      }
    }catch(Exception e){
       e.printStackTrace();
       throw new JspException(e.getMessage());
    }
    body.clearBody();
    return SKIP_BODY;
  }
}

 -----Original Message-----
From: 	Alex Tang [mailto:altitude@airflash.com] 
Sent:	Tuesday, January 30, 2001 11:49 AM
To:	taglibs-user@jakarta.apache.org
Subject:	Re: Using a jsp:include action (JSP 1.1) within a BodyTag
taglib tag

Thanks.  This sounds like a decent workaround.

Can you send examples of how you've got this working?

Does the included JSP file need to do anything 'special' to work in your
environment?

Thanks again.

...alex...

"Ward, Jeff" wrote:

>   Yup, it's part of the spec to do that. The include writes directly to
the
> request while the tag writes to the body. (which is buffered until the end
> in which case it is written to it's parent or if no parent then the
> request.) The only way I have found to get around this is to write my own
> include and included page tags.
>   My include stuffs the current out into the request and then calls the
> included page. The included page tag grabs the out from the request and
> writes to it instead of the request, the result is an inline include.
>   Another way is to have an include that uses a URL and writes the result
> inline. The upside is you don't need the included page tags but the down
> side is it isn't an include in the JSP since anymore (it has a different
> request) and it takes more resources (it is another hit to your site).
>   This is just an ugliness in the spec because they didn't think about
> nested tags that deeply.
>   Hope this helps.
>   -Jeff Ward
>
>  -----Original Message-----
> From:   Alex Tang [mailto:altitude@airflash.com]
> Sent:   Tuesday, January 30, 2001 10:16 AM
> To:     jsp-interest@java.sun.com
> Cc:     taglibs-user@jakarta.apache.org
> Subject:        Using a jsp:include action (JSP 1.1) within a BodyTag
taglib
> tag
>
> hi folks.  (Sorry for the wide distribution, this question seems to span
> both the JSP and taglib realm).
>
> I'm having a problem with the interaction of a taglib BodyTag and a
> jsp:include action (or an equivalent taglib tag that uses a
> pageContext.forward()).  For example, if I have:
>
>      <foo:myBodyTag>
>        Blah Blah...
>        <jsp:include page="..."/>
>      </foo:myBodyTag>
>
> The output from the jsp:include action always appears BEFORE the output
> from the myBodyTag taglib tag.
>
> (BTW, I'm using Tomcat 3.1)
>
> In reading the JSP 1.1 spec, section 5.4.5 says
>
>      The BodyContent Class
>
>      The BodyContent is a subclass of JspWriter that can be used to
>      process body evaluations so they can retrieved later on. The
>      class has methods to convert its contents into a String, to
>      read its contents, and to clear the contents.
>
>      The buffer size of a BodyContent object is "unbounded". A
>      BodyContent object cannot be in autoFlush mode. It is not
>      possible to invoke flush on a BodyContent object, as there is
>      no backing stream. This means that it is not legal to do a
>      jsp:include when out is not bound to the top-level JspWriter.
>
>      ...
>
> >From the last sentence, does this mean that it is illegal to use a
> jsp:include action (or an equivalent taglib tag that performs a
> pageContext.include()) within a BodyTag?
>
> Is there a way to get around this in JSP 1.1 (Specifically Tomcat 3.x)?
> Would setting the page buffer to none help?  (If so, what problems or
> drawbacks are there for settign the page buffer to none?)
>
> I've looked through the JSP 1.2 spec (although not completely), and I
> did not see any mention of this type of restriction.  Is this correct?
>
> I realize that this is somewhat contanier specific, but does anyone know
> if Tomcat 4.x allow for jsp:include actions within a BodyTag?
>
> Thank you for your time.
>
> ...alex...

Mime
View raw message