Hi,
 
I use the Include tag quiet extensively in the app I am building in order to "componentise"  the pages.  My app is initially going to run on a virtual Linux server which has very little memory so I am always concerned about optimising resources use where I can.  I was a bit worried about the implementation of the IncludeTag which always caches its output in memory buffers.  With a heavily componentised page (hierarchical) I could end up with many copies of the same jsp output in memory before the response is complete.
 
I believe from previous discussions that the only reason the IncludeTag caches the output is so that both getOutputStream and getWriter may be called from the included page.  As far as I can tell this usually should not happen as you would only use an OutputStream if you were writing binary data.  However, the spec states that either should be able to be called and some implementations may use either.
 
I have altered the current IncludeTag which was ported from WW1 to NOT buffer output. It wraps the Response and merely writes directly to the JspWriter like so:

class PageResponse extends HttpServletResponseWrapper
{
...
public PrintWriter getWriter() throws IOException
{
   
return new PrintWriter(_context.getOut());
}

public ServletOutputStream getOutputStream() throws IOException
{
   
throw new UnsupportedOperationException("Can't do this!");
}
...
}

This works great in Tomcat but will not work on servers which use getOutputStream().  I am looking at implementing a wrapper that implements OutputStream but writes to the JspWriter.  The problem is in implementing the method write(int b) as some charsets use more than one byte to represent a char.  This seems to be the reason that the current implementation buffers the entire output and then uses:

out.write(new String(bytes, encoding));

However, it would be possible to only buffer enough of the output in order to convert a few bytes into a char which can then be written to the JspWriter.

My current quick fix works great for me and reduces the memory overhead of my web app.  I can make a Jira issue with the code if anyone is interested.

Feed back on the writer wrapper idea?

John.

Reply via email to