Dreamer
Dreamer

Reputation: 7551

Can "getOutputStream() has already been called for this response" cause server crash?

I have been doing research over SO about the exception

getOutputStream() has already been called for this response

I know basically it may caused by calling getWriter() and getOutputStream() for one HttpServletResponse, as mix text and other MIME type together to return to client.

However, I am not sure if this exception occurs, and if occurs repeatingly, can cause server crash? (as the data in the buffer by getWriter() or getOutputStream() become ghost)?

Upvotes: 0

Views: 919

Answers (2)

Braj
Braj

Reputation: 46861

This is a IllegalStateException that's a RuntimeException.

RuntimeException is a unchecked exception that will not cause the system/server crash at all.


How to avoid IllegalStateException in java servlet?

The root cause of IllegalStateException exception is a java servlet is attempting to write to the output stream (response) after the response has been committed.

It is always better to ensure that no content is added to the response after the forward or redirect is done to avoid IllegalStateException. It can be done by including a return statement immediately next to the forward or redirect statement.


For more solutions have a look at JSP-Servlet Q&A » Development » IllegalStateException

Upvotes: 2

pingw33n
pingw33n

Reputation: 12510

I'd say it depends on the servlet container but normally exceptions inside the servlet code don't crash the whole server.

If you're worrying about a posible memory leak here it's also unlikely to happen in this case. This exception is just a contract enforcement. The servlet container tells you that you should use either getOutputStream() or getWriter() but never both during a single request.

Upvotes: 1

Related Questions