Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
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
ponse
at org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:599)
at org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacad
e.java:163)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:122)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:115)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFacto
ryImpl.java:115)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.j
ava:75)
at org.apache.jsp.pages.imageret_jsp._jspService(imageret_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
24)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:237)
***************************************************************************

Here is my code for displaying the image from the database

try
{
Integer id_user=(Integer)session.getAttribute("id_user");
Class.forName("com.mysql.jdbc.Driver");
Connection con = java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/eh","root","admin");

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

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

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.

Thanks

With Regards
S.R.K.Vivek Raju.
 
Shyamsundar Shounak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
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.

Shyam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic