Page 1 of 1

About Question com.enthuware.ets.scwcd.v5.2.5 :

Posted: Mon Oct 27, 2014 9:16 pm
by bluster
The explanation says it will not throw an exception because "As per the API of the sendError() method, if you write to the response after calling sendError() then the data is ignored and no exception is thrown."

Below is the quote from the API. It does not directly agrees with the above, and to me it throws doubt if we would not get an IllegalStateException writing to the resource after the sendError().

Can someone comment on this. In practical (exam!) terms, should one consider that an out.println() of some data after the sendError will throw an exception, if the buffer has been large enough to have so far avoided a commit?

API:
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
**
http://docs.oracle.com/javaee/5/api/jav ... ng.String)

Re: About Question com.enthuware.ets.scwcd.v5.2.5 :

Posted: Mon Oct 27, 2014 10:31 pm
by admin
It is true that the sendError method throws exception if the response is already committed. But the explanation is talking about what happens if you write to the response after calling sendError.
As per Section SRV 5.3 of Servlet 2.4:
The following convenience methods exist in the HttpServletResponse interface:
• sendRedirect
• sendError
....
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.
When the question was developed, the JavaDoc API documentation probably said the same thing and has now been changed or the question author took the information from the specification and not from the JavaDoc.
In case of contradictory statements, we go with the specification.

HTH,
Paul.

Re: About Question com.enthuware.ets.scwcd.v5.2.5 :

Posted: Tue Oct 28, 2014 10:28 am
by bluster
Thanks for the prompt reply.

To wrap for future visitors, there are two possible sequences:

1) sendError(), then getWriter().println("somedata") -> "somedata" output will be ignored.

2) getWriter().println("somedata") then sendError() -> branches into two possibilities:
2a) response buffer not committed -> "somedata" output ignored
2b) response buffer commited -> sendError() will throw IllegalStateException.

Please confirm, if possible. Thanks again.

Re: About Question com.enthuware.ets.scwcd.v5.2.5 :

Posted: Tue Oct 28, 2014 11:40 am
by admin
That is correct.