Problem with OutputStream
I've read Servlet Exception - getOutputStream() has already been called for this response but I didn't found any solution for my problem.
In my main.jsp I've this statement:
<img src="ImageElaborator.jsp" style="float: left" alt="" height="70" width="70"/>
and in my ImageElaborator.jsp:
byte[] photo = getPhoto();
response.g开发者_高级运维etOutputStream().write(photo, 0, photo.length);
This snippet shows my image, but I've this error:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
I don't understand how I can solve it. Please help!
Don't do this in a JSP. Do it in a servlet.
You shouldn't put such code in a JSP, because at the time the code is executed, some blank spaces, newlines, etc. at the beginning of the JSP have probably already been sent to the output stream of the JSP.
JSP should be used to output text or markup, but not to execute business logic and send raw bytes to the output stream. You should put this kind of code in a servlet, or in an action of your favorite MVC framework (Struts, Stripes, Spring MVC, etc.)
In a JSP you're not supposed to call the OutputStream as it is defined as implicit variable see implicit session and objects: http://www.exforsys.com/tutorials/jsp/jsp-implicit-and-session-objects.html
I reckon something like that should be OK:
byte[] photo = getPhoto();
out.write(photo, 0, photo.length);
But the best way to do is using a Servlet as it has been said.
精彩评论