File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt in Exception occured by sendError() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt in Exception occured by sendError()" Watch "doubt in Exception occured by sendError()" New topic

doubt in Exception occured by sendError()

aleyx chow

Joined: May 15, 2005
Posts: 16
Why the code followed doesnt occur an IllegalStateException?

the output on IE6.0 (tomcat5.5,jdk1.5,servlet2.4)is:

test sendError()
is committed?false
is committed?true
//the String "sendError() method is called." is not printed
//the page isnt displayed as a 404 error page
//an IllegalStateException isnt thrown

is the output meaning that:
after flushBuffer() is called, the response is committed and
after sendError() is called, the buffer is reset?
but why an IllegalStateException is NOT occured?

i read servlet2.4 api doc,it says:

public void sendError(int sc)
Throws: - If an input or output exception occurs
IllegalStateException - If the response was committed before this method call

but it still puzzled my brains, isnt the sendError() method called after the response is committed in my code? Why an Exception isnt thrown
[ August 18, 2005: Message edited by: aleyx chow ]

Oh,father,though im in the nutshell,i still the king of limitless space.<br /> <br />scjp 1.4,scwcd 1.4,scbcd 1.3
Sergey Tyulkin
Ranch Hand

Joined: May 10, 2005
Posts: 87
What web container do you use?
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215

If you are using Tomcat look in the logs on my machine Tomcat5.0/logs/localhost_log.2005-08-18.txt shows an exception was thrown. I have used the code you provided above to get the exception.

Sorry I can't explian why you get the line - I also get it!
is committed?true

aleyx chow

Joined: May 15, 2005
Posts: 16
hi,thx 4 your replies.

To Sergey Tyulkin: i used Tomcat 5.5 and the browser is IE 6.0

To Mat Williams:
yes, i also found the "throw exception" record in the tomcat log files, thank u for pointing out that, but in my thought, the result on the web browser would also display the follows:

HTTP Status 500 -
type Exception report
description The server encountered an internal error () that prevented it from fulfilling this request.

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.

i found the reason that the browser is blank is i called flushBuffer() before throw an exception.

will show the exception's details on the browser and record it in the blog files, but

will not show anything on browser.
Why?What's the mechainism of the servlet output stream to do this?
Maybe it's another question and i should post it as a new topic, but i think it is related to the old one.
Sergey Tyulkin
Ranch Hand

Joined: May 10, 2005
Posts: 87
When you call sendError(), you automatically close the response, and all data, passed after sendError() is ignored.
To show 404 page i think you should define <error-page> element in DD with 404 eror-code.
aleyx chow

Joined: May 15, 2005
Posts: 16
thanx 4 u replay,:-)
I agree. Here's the link:
subject: doubt in Exception occured by sendError()
It's not a secret anymore!