Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

S.R.K.Vivek Raju
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!