I get the following exception when I'm trying to request loading images from server on client side:
241132533 [TP-Processor1] ERROR [/jspapps].[jsp] - Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response
Can any one explain this exception to me and also how to get over it?
I just stumbled upon this old question as I had the same issue. In the end it was quite easy to get rid of the exception: Just call
out.clear()
before:out.clear()
also helped me to get rid of all those empty lines from<%@page import=...
and the like.I had this code and fixed it like this:
I returned null because, from this method, I was expecting a download. As per the explanation given here, I fixed my problem.
Make sure eliminating all output in your
view_image.jsp
. Simple line breaks can be responsible for generating output.For example, if you have these declarions...
... you should write them this way
If you take a look to the compiled servlet code you shouldn't see
out.write("\r\n")
before your image response.A better way would be to change your
view_image.jsp
into a Servlet, but if you can't do that, removing the line breaks in the jsp is a workaround.You're attempting to write binary data to
response.getOutputStream()
using raw Java code inside a JSP file which itself is already usingresponse.getWriter()
to write any template text. See also the Throws part of the linked javadocs.Write Java code in a real Java class instead. Create a class which
extends
HttpServlet
, move all that Java code to there, map it inweb.xml
and change the request URL to call the servlet instead.See also: