tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Len Popp" <len.p...@gmail.com>
Subject Re: Avoiding same server to server HTTP calls to generate HTML pages via JSPs
Date Fri, 04 Jan 2008 21:54:28 GMT
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 <gshumakher@gmail.com> 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:miki@ceti.pl]
> 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 <miki@ceti.pl>
>
>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message