Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response committed at which line ??

 
Jeffrey Pony
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

if(req.getAttribute("me")==null){
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
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
IllegalStateException must be thrown since the response is already committed.
Thanks
Ravi
 
Mark Bensing
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeffrey Pony
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic