CeccoCQ
CeccoCQ

Reputation: 3754

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.getOutputStream().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!

Upvotes: 0

Views: 580

Answers (3)

art1go
art1go

Reputation: 89

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.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691865

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

Upvotes: 1

Bozho
Bozho

Reputation: 597164

Don't do this in a JSP. Do it in a servlet.

Upvotes: 1

Related Questions