response committed at which line ??

Hi.. 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

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. Mark [ March 25, 2003: Message edited by: Mark Bensing ]

Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40

posted Mar 25, 2003 21:37:00

0

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. Mark