You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2009/05/01 22:01:51 UTC

Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave,

On 4/29/2009 10:41 PM, Dave Cherkassky wrote:
> 1) I agree with you for includes and forwards, but for errors?  I would
> have thought that the definition of "error" is that something went
> wrong, and Tomcat should start from scratch and just render the error
> page.  I didn't think of it as really a forward...
> 
> 2) So you are suggesting that writing out to the
> response.getOutputStream() as I go is wrong?  Instead, I need to:
> a. buffer my output locally
> b. once I am sure that it can all be created correctly, *then* I write
> out the buffer to the response?

The buffer isn't the problem, as Pid suggests. If it were, you'd be
getting a different error (something like "Response already committed").

The problem is that your servlet calls response.getOutputStream() and
your error page (a JSP) calls response.getWriter(). The servlet spec
says this is a no-no, so you get this exception thrown. It's not really
your fault per se, but that's what's happening.

Do you have the option of generating your Excel document to memory
(ByteArrayOutputStream) or a temporary file before streaming it to the
client? That would be ideal, as you could wait until the entire document
was generated before trying to send it (or any of the "Content-Type"
headers, which would be helpful). If an error occurs, the user gets an
error page instead of an error page disguised as an Excel document.

If the document you generate is huge, and you *must* stream it, well,
then, you just have to do a better job of error handling in your
servlet, then. Just catch Throwable or RuntimeException or whatever and
take appropriate action (whatever that means... maybe putting an error
directly in the Excel file like "OMG something bad happened... file
truncated").

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkn7VS8ACgkQ9CaO5/Lv0PAPVwCdGEsKrB1kTjAfngKtp0HDnO6s
X9gAoKwLjyViqjF2FtSALiCsqjT/dvFa
=iThm
-----END PGP SIGNATURE-----

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


Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dave,

On 5/4/2009 12:36 PM, Dave Cherkassky wrote:
> My typical naming convention is to use "out" as a variable for
> OutputStream.  But of course that's not the case here -- "out" is a Writer!

Yup. Now, upgrade! ;) You can tell your politicians that TC 4.1 will
"soon" be unsupported and that it's really time to move up (and it
really is... I recently moved from 4.1 to 5.5 and it really was a breeze).

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkn/TwoACgkQ9CaO5/Lv0PBI7wCgj8ZB/UjNIP8Q2ZdWgtK0BmNJ
1TwAoJ1a5bje/OR/umBCFvqlRC5ndV7p
=PODB
-----END PGP SIGNATURE-----

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


Re: Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Dave Cherkassky <dc...@djinnsoft.com>.
Chris, you are 100% right.

I got caught up in my own assumptions.

When I examined Tomcat's generated java class, I saw code like this:
  out.print( "...." );

My typical naming convention is to use "out" as a variable for OutputStream.  But of course that's not the case here -- "out" is a Writer!


Thanks for your help,
-- 
Dave Cherkassky
  VP of Software Development
  DJiNN Software Inc.
  416.504.1354


Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Dave,
> 
> On 4/29/2009 10:41 PM, Dave Cherkassky wrote:
>> 1) I agree with you for includes and forwards, but for errors?  I would
>> have thought that the definition of "error" is that something went
>> wrong, and Tomcat should start from scratch and just render the error
>> page.  I didn't think of it as really a forward...
>>
>> 2) So you are suggesting that writing out to the
>> response.getOutputStream() as I go is wrong?  Instead, I need to:
>> a. buffer my output locally
>> b. once I am sure that it can all be created correctly, *then* I write
>> out the buffer to the response?
> 
> The buffer isn't the problem, as Pid suggests. If it were, you'd be
> getting a different error (something like "Response already committed").
> 
> The problem is that your servlet calls response.getOutputStream() and
> your error page (a JSP) calls response.getWriter(). The servlet spec
> says this is a no-no, so you get this exception thrown. It's not really
> your fault per se, but that's what's happening.
> 
> Do you have the option of generating your Excel document to memory
> (ByteArrayOutputStream) or a temporary file before streaming it to the
> client? That would be ideal, as you could wait until the entire document
> was generated before trying to send it (or any of the "Content-Type"
> headers, which would be helpful). If an error occurs, the user gets an
> error page instead of an error page disguised as an Excel document.
> 
> If the document you generate is huge, and you *must* stream it, well,
> then, you just have to do a better job of error handling in your
> servlet, then. Just catch Throwable or RuntimeException or whatever and
> take appropriate action (whatever that means... maybe putting an error
> directly in the Excel file like "OMG something bad happened... file
> truncated").
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAkn7VS8ACgkQ9CaO5/Lv0PAPVwCdGEsKrB1kTjAfngKtp0HDnO6s
> X9gAoKwLjyViqjF2FtSALiCsqjT/dvFa
> =iThm
> -----END PGP SIGNATURE-----
> 


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


Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

On 5/1/2009 5:34 PM, Mark Thomas wrote:
> Long story short.
> 
> Old error handling: Reset the response. Leave usingWriter flag alone.
> New error handling: Reset the buffer. Reset usingWriter flag.

Gotcha. You're right: I hadn't read /everything/ in the bug. I thought I
had read enough (which was more than just the description, I promise :)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkn/To8ACgkQ9CaO5/Lv0PByawCfYahCb8+dydkIwkxyR0Qo6dyL
r+sAnRjHTOg1c7x9jSCAZsbwSckqF8i7
=Ahm5
-----END PGP SIGNATURE-----

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


Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Mark Thomas <ma...@apache.org>.
Dave Cherkassky wrote:
> Chris,
> 
> Your interpretation is correct.  I am not having any troubles with headers.

There is method to my madness ;)
You need to read the bug and associated svn commits in detail.

> So I think that this line of investigation is a dead end.

Long story short.

Old error handling: Reset the response. Leave usingWriter flag alone.
New error handling: Reset the buffer. Reset usingWriter flag.

Tomcat 4 is using the old method. Hence the problem you are seeing.

Mark


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


Re: Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Dave Cherkassky <dc...@djinnsoft.com>.
Chris,

Your interpretation is correct.  I am not having any troubles with headers.

So I think that this line of investigation is a dead end.

Thanks,
-- 
Dave Cherkassky
  VP of Software Development
  DJiNN Software Inc.
  416.504.1354

Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Mark,
> 
> On 5/1/2009 4:21 PM, Mark Thomas wrote:
>> As I replied a couple of days ago. This is almost certainly a known
>> Tomcat issue.
>>
>> http://markmail.org/message/dmsf24ppd2nopvnz
> 
> My interpretation of the OP was that the exception ("getOutputStream
> already called") was unexpected. The custom header thing hadn't (yet)
> come up.
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAkn7W4MACgkQ9CaO5/Lv0PB5pgCfWkwTtUWzTbrZYyEqeF8aZxQ5
> yP4AnRpmZcckIoZTQ/s4efMHDjluYBfK
> =pXBO
> -----END PGP SIGNATURE-----
> 



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


Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

On 5/1/2009 4:21 PM, Mark Thomas wrote:
> As I replied a couple of days ago. This is almost certainly a known
> Tomcat issue.
> 
> http://markmail.org/message/dmsf24ppd2nopvnz

My interpretation of the OP was that the exception ("getOutputStream
already called") was unexpected. The custom header thing hadn't (yet)
come up.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkn7W4MACgkQ9CaO5/Lv0PB5pgCfWkwTtUWzTbrZYyEqeF8aZxQ5
yP4AnRpmZcckIoZTQ/s4efMHDjluYBfK
=pXBO
-----END PGP SIGNATURE-----

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


Re: Servlets that use response.getOutputStream(): do they play nicely with Tomcat's error pages?

Posted by Mark Thomas <ma...@apache.org>.
Folks,

As I replied a couple of days ago. This is almost certainly a known
Tomcat issue.

http://markmail.org/message/dmsf24ppd2nopvnz

Mark




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