On Fri, 19 Oct 2001, Lessault Jean-Luc wrote:

> Date: Fri, 19 Oct 2001 16:03:50 +0200
> From: Lessault Jean-Luc <[EMAIL PROTECTED]>
> Reply-To: [EMAIL PROTECTED]
> To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
> Subject: To get back content from getRequestDispatcher()...
>
>
> Hi!
>
> Is there any way to get back the content from :
>
>         getServletContext().getRequestDispatcher(/filename.jsp).forward();
> (or include())
>
> or to read results who appears in a browser.
>
> Thanks to all.
>
>

In a Servlet 2.3 environment (i.e. Tomcat 4) you can indeed capture the
output from a RequestDispatcher.include() call.  (Trying this on a forward
is going to cause you different sorts of difficulties, so don't bother).
The basic idea is that you must create a wrapper around the response
object that you hand to the include() method, and override the output
methods in the response so that the included servlet actually writes to a
memory buffer instead of to the "real" response.

There was a recent "Tech Tip" example on the Java Developer Connection
site <http://developer.java.sun.com> about using this approach to perform
an XSLT transformation on the output that was captured.  Although this
example used a Filter, you can also do the same kind of thing inside a
servlet before calling RequestDispatcher.include().

There are also some custom tag libraries in the Jakarta Taglibs project
<http://jakarta.apache.org/taglibs> that do this sort of thing for you
using JSP custom tags.  In particular, check out the "Scrape" and "XTags"
libraries for some interesting techniques.

Craig


Reply via email to