You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Graham Lea <Gr...@forge.com.au> on 2000/07/06 04:45:56 UTC

Using include() in a custom tag implementation.

I'm trying to include() a JSP page through a custom tag with a call like:

pageContext.getRequest().getRequestDispatcher( "page.jsp"
     ).include( pageContext().getRequest(), pageContext.getResponse() );

It works, except that it writes the result of the include()ed JSP out
BEFORE it writes out the HTML on the page originally calling the tag.
So, if my custom tag printed this:

     <head><title><%= title %></title></head>

and I used it like this:

<html>
<customTags:head title="Foo"/>
<body>
     The title is Foo.
</body>
</html>

then the source on the client appears like this:

     <head><title>Foo</title></head>
<html>
<body>
     The title is foo.
</body>
</html>


Would like to know

     (1) Why this is happenning and
     (2) Is this the expected behaviour?

Thanks,

Graham Lea
Forge Research Pty Ltd


Re: Using include() in a custom tag implementation.

Posted by Jochen Schneider <jo...@decrc.abb.de>.
Hi,

funny yesterday I had the same problem:

You have to flush the outputstream of the pageContext before you include
another JSP page. This will solve the problem. Use the include methode of
the pageContext variable instead of obtaining a RequestDispatcher. See
below:

  JspWriter out = pageContext.getOut();
  out.println("...");
  ....
  out.flush();
  /** include something here*/
  pageContext.include("page.jsp");
  /* go on ...*/



If I understand the javadoc of the servlet api 2.2 right the flushing of the
output buffer shouldn't be necessary. Probably a bug in Tomcat ?

Javadoc of PageContext:

include
public abstract void include(java.lang.String relativeUrlPath)
throws ServletException,         java.io.IOException

Causes the resource specified to be processed as part of the current
ServletRequest and ServletResponse being processed by the calling Thread.
The output of the target resources processing of the request is written
directly to the ServletResponse output stream.

The current JspWriter "out" for this JSP is flushed as a side-effect of this
call, prior to processing the include.

If the relativeUrlPath begins with a "/" then the URL specified is
calculated relative to the DOCROOT of the ServletContext for this JSP.
If the path does not begin with a "/" then the URL specified is calculated
relative to the URL of the request that was mapped to the calling JSP.



Hope this helps,

Jochen


----- Original Message -----
From: Graham Lea <Gr...@forge.com.au>
To: <to...@jakarta.apache.org>
Sent: Thursday, July 06, 2000 4:45 AM
Subject: Using include() in a custom tag implementation.


> I'm trying to include() a JSP page through a custom tag with a call like:
>
> pageContext.getRequest().getRequestDispatcher( "page.jsp"
>      ).include( pageContext().getRequest(), pageContext.getResponse() );
>
> It works, except that it writes the result of the include()ed JSP out
> BEFORE it writes out the HTML on the page originally calling the tag.
> So, if my custom tag printed this:
>
>      <head><title><%= title %></title></head>
>
> and I used it like this:
>
> <html>
> <customTags:head title="Foo"/>
> <body>
>      The title is Foo.
> </body>
> </html>
>
> then the source on the client appears like this:
>
>      <head><title>Foo</title></head>
> <html>
> <body>
>      The title is foo.
> </body>
> </html>
>
>
> Would like to know
>
>      (1) Why this is happenning and
>      (2) Is this the expected behaviour?
>
> Thanks,
>
> Graham Lea
> Forge Research Pty Ltd
>
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
>