You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@click.apache.org by Bob Schellink <sa...@gmail.com> on 2010/03/13 05:57:06 UTC

getOutputStream() has already been called for this response

Hi all,

In our applications we often render PDF's to the response's outputStream. Click templates of the 
other hand uses the response's writer.

If an exception is thrown after response.getOutputStream is called, Click will try and render the 
ErrorPage using the response.getWriter which leads to the exception:

   getOutputStream() has already been called for this response

Is there an easy way to determine whether its safe to invoke getWriter?

kind regards

bob

Re: getOutputStream() has already been called for this response

Posted by Bob Schellink <sa...@gmail.com>.
Hi Henry,

On 13/03/2010 06:25 PM, Henry Saputra wrote:

> , OR fallback to use ServletOutputStream if IllegalStateException is
> triggered during call to getWriter:
>               new OutputStreamWriter(response.getOutputStream(),
> response().getCharacterEncoding());


Agreed, a try/catch block which wraps the outputStream in a writer should work well.

I'll try and come up with a patch for VelocityTemplateService and FreemarkerTemplateService.

kind regards

bob

Re: getOutputStream() has already been called for this response

Posted by Henry Saputra <he...@gmail.com>.
HI Bob,

Looks like we cant call getWriter when getOutputStream has been called to
write to body in a same request.

Maybe we could catch IllegalStateException when calling request.getWriter
and the code could redirect with something like
          response.sendRedirect(request.getContextPath() +
"/click/error.htm")
, OR fallback to use ServletOutputStream if IllegalStateException is
triggered during call to getWriter:
             new OutputStreamWriter(response.getOutputStream(),
response().getCharacterEncoding());

- Henry

On Fri, Mar 12, 2010 at 8:57 PM, Bob Schellink <sa...@gmail.com> wrote:

> Hi all,
>
> In our applications we often render PDF's to the response's outputStream.
> Click templates of the other hand uses the response's writer.
>
> If an exception is thrown after response.getOutputStream is called, Click
> will try and render the ErrorPage using the response.getWriter which leads
> to the exception:
>
>  getOutputStream() has already been called for this response
>
> Is there an easy way to determine whether its safe to invoke getWriter?
>
> kind regards
>
> bob
>