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

Moderator: admin

Post Reply
bluster
Posts: 60
Joined: Wed Apr 23, 2014 6:38 pm
Contact:

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

Post 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)
Last edited by admin on Mon Oct 27, 2014 10:32 pm, edited 1 time in total.
Reason: Fixed issue with redirect.

admin
Site Admin
Posts: 10068
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

bluster
Posts: 60
Joined: Wed Apr 23, 2014 6:38 pm
Contact:

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

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

admin
Site Admin
Posts: 10068
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 126 guests