tags:

views:

48

answers:

4

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?

+1  A: 

can any one explain this exception to me

You're attempting to write binary data to response.getOutputStream() using raw Java code inside a JSP file which itself is already using response.getWriter() to write any template text. See also the Throws part of the linked javadocs.

and also how to get over it?

Write Java code in a real Java class instead. Create a class which extendsHttpServlet, move all that Java code to there, map it in web.xml and change the request URL to call the servlet instead.

See also:

BalusC
sorry but I need to put my code in jsp file ,and i load the image by <img src='view_image.jsp?pat_acc=<%=Pat_Acct%>' style='position: absolute; left: 0pt; top: 0px;' "/>
ama
@ama: Turn view_image.jsp into a Servlet mapped to `viewImage` and call it like `<img src='viewImage?pat_acc=<%=Pat_Acct%>' style='position: absolute; left: 0pt; top: 0px;' "/>` in your JSP file.
Bytecode Ninja
That's correct. The problem is in `view_image.jsp`. It ought to be replaced by a servlet class.
BalusC
A: 

Turn view_image.jsp into a Servlet mapped to ViewImage and call it like

<img src='<%= request.getContextPath() %>/ViewImage?pat_acc=<%=Pat_Acct%>' style='position: absolute; left: 0pt; top: 0px;' "/> 

in your JSP file.

Bytecode Ninja
A: 

try remove all template texts from jsp file. for example,

1 <%@
2    ....
3 %>
4 <%
5    ....
6 %>

there is a '\n' between line 3 and 4, and it is treated as template text, response.getWriter() is called to write that '\n' to client. after line 6, there could be invisible whitespaces too which will screwup the outputstream. but line 5 can return early to avoid that.

irreputable
A: 

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

<%@ page import ... %>
<%@ page import ... %>

... you should write them this way

<%@ page import ... %><%@ page import ... %><%
...%>

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.

Soundlink