my dog learned polymorphism
The moose likes Struts and the fly likes ERROR : java.lang.Illegal State Exception :  getOutputStream()  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "ERROR : java.lang.Illegal State Exception :  getOutputStream()  " Watch "ERROR : java.lang.Illegal State Exception :  getOutputStream()  " New topic

ERROR : java.lang.Illegal State Exception : getOutputStream()

S.R.K.Vivek Raju
Ranch Hand

Joined: Sep 23, 2004
Posts: 58
Hi all

I am uploading images and storing into mysql database, after i upload and refresh the page for display the image it gives the following error.

16:28:03,406 ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet js
p threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this res
at org.apache.coyote.tomcat5.CoyoteResponse.getWriter(
at org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacad
at org.apache.jasper.runtime.JspWriterImpl.initOut(
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(
at org.apache.jasper.runtime.PageContextImpl.release(
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFacto
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.j
at org.apache.jsp.pages.imageret_jsp._jspService(
at org.apache.jasper.runtime.HttpJspBase.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.jasper.servlet.JspServletWrapper.service(
at org.apache.jasper.servlet.JspServlet.serviceJspFile(
at org.apache.jasper.servlet.JspServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application

Here is my code for displaying the image from the database

Integer id_user=(Integer)session.getAttribute("id_user");
Connection con = java.sql.DriverManager.getConnection("jdbc:mysql://","root","admin");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select img from userimage where id="+id_user.intValue());

response.setContentType("image/gif") ;
byte[] b = rs.getBytes(1);
ServletOutputStream x = response.getOutputStream();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@The image");
catch (Exception e)

I tried flushing and still the error comes up on my jboss console, i am using struts 1.2.4 if that helps.

It would be really great if someone could guide me through this.


With Regards
S.R.K.Vivek Raju.
Shyamsundar Shounak

Joined: Jan 26, 2009
Posts: 1
You can not write anything to the stream after it is flushed. The stream is flushed means that response is sent and once the response is sent you can not write any more content to it.

If you attempt it, you get IllegalStateException. Put the flush statement after the while loop and things will work.

Happy coding.

I agree. Here's the link:
subject: ERROR : java.lang.Illegal State Exception : getOutputStream()
It's not a secret anymore!