Here's some code that does what Gennady suggested, using
RequestDispatcher and wrapping the response object to capture the
output in a String.
public class JSPFormatter
{
public String formatPage(String stJSPFile,
HttpServletRequest request,
HttpServletResponse response,
ServletContext ctx)
throws ServletException, IOException
{
// Make a wrapper for the response, to capture the output.
ResponseWrapper rw = new ResponseWrapper(response);
// Call the JSP page and capture its output.
RequestDispatcher rd = ctx.getRequestDispatcher(stJSPFile);
rd.include(request, rw);
return rw.getOutputString();
}
/**
* Nested class ResponseWrapper: Wrapper for the response to
capture the servlet output.
*/
public class ResponseWrapper extends HttpServletResponseWrapper
{
protected StringWriter writer = null;
protected ByteArrayOutputStream stream = null;
public ResponseWrapper(HttpServletResponse response) {
super(response);
}
public String getOutputString() {
if (writer != null)
return writer.toString();
else if (stream != null)
return stream.toString();
else
return null;
}
public ServletOutputStream getOutputStream() throws IOException {
stream = new ByteArrayOutputStream();
return new OutputStreamWrapper();
}
public PrintWriter getWriter() throws IOException {
writer = new StringWriter();
return new PrintWriter(writer);
}
/**
* Nested class OutputStreamWrapper: Wrapper for the
OutputStream to turn it
* into a ServletOutputStream for getOutputStream.
*/
public class OutputStreamWrapper extends ServletOutputStream
{
public void write(int b) throws IOException {
stream.write(b);
}
}
}
}
--
Len
On Jan 4, 2008 4:15 AM, Gennady Shumakher <[EMAIL PROTECTED]> wrote:
> If you cannot replace your jsps with velocity (or freemarker) you could
> still use RequestDispatcher. Just provide your own implementation of
> ServletResponse that mostly will be just wrapper based on servlet response
> instance ( Tomcat implementation), but will buffer response output as
> string rather than write it to output stream.
>
> Gennady.
>
> -----Original Message-----
> From: Mikolaj Rydzewski [mailto:[EMAIL PROTECTED]
> Sent: Friday, January 04, 2008 10:15
> To: Tomcat Users List
> Subject: Re: Avoiding same server to server HTTP calls to generate HTML
> pages via JSPs
>
>
> Adam Gordon wrote:
> > Right now we basically have a URL dispatcher that when a specific
> > request comes in, we make another request to retrieve the contents of
> > a URI (a JSP page to generate HTML) via a server-to-server HTTP call
> > and then essentially slap that into a MimeBodyPart for sending
> > text/html email messages.
> Simply do not use JSP. Use Velocity for example.
>
> --
> Mikolaj Rydzewski <[EMAIL PROTECTED]>
>
>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: [email protected]
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To start a new topic, e-mail: [email protected]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]