2 Replies Latest reply on Jun 8, 2012 3:29 AM by samwun9988

    IllegalStateException

    samwun9988

      I am trying to invoke database directly in JSP, but I encountered the following error:

       

       

      error :java.lang.IllegalStateException

       

      the above error is caused by the following statement in my jsp file:

       

       

      OutputStream os = response.getOutputStream();

               os.write(bytearray,0,size);

               os.flush();

       

       

      Full jsp code is:

       

       

      <%@page import="java.sql.*,java.io.*"%>

      <%

      byte[] bytearray = null;

      try{

          InputStream sImage;

          Class.forName("com.mysql.jdbc.Driver");

          Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/houseware","root","xxx");

            Statement st=con.createStatement();

            ResultSet rs=st.executeQuery("SELECT image_data FROM category_images WHERE category_id=4957");

            out.println(" -- before rs.last() --");

            if(rs.last()) {

                  bytearray = new byte[1048576];

                  int size=0;

                  sImage = rs.getBinaryStream(1);

                  response.reset();

                  response.setContentType("image/jpeg");

                  out.println("--- before while() --- ");

                  while((size=sImage.read(bytearray))!= -1 ) {

                      out.println("-- in while() --");

                      OutputStream os = response.getOutputStream();

                      os.write(bytearray,0,size);

                      os.flush();

                  }

            }

            con.close();

      }   

          catch(Exception ex){

          out.println("bytearray:"+bytearray.length);

          out.println("error :"+ex);

      }

      %>

       

       

      Any suggestion would be very appreciated.

      Sam