aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes response committed at which line ?? 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 "response committed at which line ??" Watch "response committed at which line ??" New topic

response committed at which line ??

Jeffrey Pony

Joined: Jun 24, 2002
Posts: 25
This one is straight from the scwcd exam kit book
by Hanumant Deshmukh and Jignesh Malavia
( chapter 7 : review questions[2] ).
Iam not sure why the scwcd book says that the response is committed at \\2,
I thought response is committed at \\1 as the response has been sent and an IllegalStateException
would be generated at \\2 and not at \\3 as stated in the book.
Kindly enlighten me
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
PrintWriter pw = res.getWriter();
pw.println("<p>helloooooooo</p>"); \\1

res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ".......iam getting null"); \\2

pw.println("<p> done </p>"); \\3

To my surprise, when I ran this example I got the output :
done followed by the generated html error page by the container
but no IllegalStateException as expected
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
IllegalStateException must be thrown since the response is already committed.

Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
The response is not commited at line 1 in the example code that you posted. The response is not commited until data is actually sent back to the client. Since the response PrintWriter is usually buffered, the response will not be commited until the buffer is flushed either because it was filled or because flush() was explicitly called on the PrintWriter.
When running example code, I got the same results as you did on Tomcat 4.0. Here is an excerpt from the Servlet specification SRV.5.3:
These methods {referring to sendRedirect and sendError} will have the side effect of committing the response, if it has not already been committed, and terminating it. No further output to the client should be made by the servlet after these methods are called. If data is written to the response after these methods are called, the data is ignored.

Note that this does not say that an IllegalStateException will be thrown, but it does indicate that the String "done" should not have been returned to the browser (maybe a Tomcat bug?). Based on this, I believe the answer in the back of the book is incorrect IMHO.
[ March 25, 2003: Message edited by: Mark Bensing ]
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
An update on this - I just tried running the servlet on Tomcat 4.1.18 and the String "done" does not get returned to the browser; only the error page is shown. This is what I would expect to happen according to the servlet spec.
Jeffrey Pony

Joined: Jun 24, 2002
Posts: 25
Thanks Mark
I agree. Here's the link:
subject: response committed at which line ??
Similar Threads
errata SCWCD Study Kit
sendRedirect problem
Code Review - ChatServlet
scwcd doubt on jsp:forward
Forward does now give exception