4 Replies Latest reply on Oct 29, 2004 5:45 PM by Joan Horta Tosas

    Image Retrieved from DB, doeesnt get displayed correctly in

    robin bajaj Newbie

      Summary ::
      Why is my browser not showing my constructed image
      Retrieved image from DB , constructed using ImageIcon

      Details

      My Servlet code is retrieving image using resultset.getBlob() method
      and then constructs an ImageIcon from it using the following code.
      But when i display it using <img src=imageicon> html tag
      the image doesnt get displayed and the browser shows an empty placeholder instead of image.?
      Please help !!!
      (P.s: rest of the printed parameters show correct values confirming an image is
      constructed correctly , but i dont know how to display it in the browser.)

      while(rs.next()){
      System.out.println("hi there");
      s=rs.getString("filename");

      image=rs.getBlob(3);

      System.out.println("filename "+s);
      System.out.println("image"+image);

      iLength = (int)image.length();

      System.out.println("ilength="+iLength);

      ii = new ImageIcon(image.getBytes( 1, iLength ));
      System.out.println("sucessflag");

      out.println("here's the icon");
      out.println("");


      }

      ///output on the jboss application server console for the


      System.out.println() statements
      12:51:34,504 INFO [STDOUT] hi there
      12:51:34,504 INFO [STDOUT] filename jchatbox.gif
      12:51:34,504 INFO [STDOUT] imagecom.mysql.jdbc.Blob@1031310
      12:51:34,504 INFO [STDOUT] ilength=5779
      12:51:34,624 INFO [STDOUT] sucessflag

        • 1. Re: Image Retrieved from DB, doeesnt get displayed correctly
          Craig Berry Newbie

          Rather than embedding a reference to the image data, you need to return a properly formed response containing the correct http headers, then the image data. Here's an excerpted example from one of my servlets that does this:

           ImageDTO imageValue =
           ResourceFacadeDelegate.getInstance().getImageDTO(imageKey);
          
           response.setContentType(imageValue.getMimeType());
           response.getOutputStream().write(imageValue.getImageData());
          


          • 2. Re: Image Retrieved from DB, doeesnt get displayed correctly
            robin bajaj Newbie

            hi Craig
            Okay i have changed my code the following way (in the bold) to take care of the appropriate content type i am showing on the html page(since i only have one gif file , i know the content type should be image/gif .
            since the code you gave..probably uses your own custom-classes like ImageDTO...(i think so ...so i couldnt use that for my problem-solution)


            while(rs.next()){
            System.out.println("hi there");
            s=rs.getString("filename");

            image=rs.getBlob(3);

            System.out.println("filename "+s);
            System.out.println("image"+image);

            iLength = (int)image.length();

            System.out.println("ilength="+iLength);
            ii = new ImageIcon(image.getBytes( 1, iLength ));
            System.out.println("sucessflag");

            out.println("here's the icon");
            response.setContentType("image/gif");
            out.println("");
            response.setContentType("text/html");
            out.println("");


            When i do it as above, the image still doenst show up and when i right click to do View image in the browser , i get Tomcat error page saying

            HTTP Status 404 - /timeraccess/servlet/javax.swing.ImageIcon@85e57
            
            type Status report
            
            message /timeraccess/servlet/javax.swing.ImageIcon@85e57
            
            description The requested resource (/timeraccess/servlet/javax.swing.ImageIcon@85e57) is not available.
            Apache Tomcat/5.0.28


            so kindly help me with either understanding your code snippet as to what package are the classes ImageDTO ,ResourceFacadeDelegate,imageKey belong to .

            or kindly help me understand what's wrong with the code above assuming i know for sure, its always going to be an image/gif
            type of image,that i am trying to display.

            thanks in advance ...loooking forward to hearing from you soon.
            regards
            robin

            • 3. Re: Image Retrieved from DB, doeesnt get displayed correctly
              robin bajaj Newbie

              hi craig
              As you suggested(as i concluded lookinga at your code as of what i can do using the java package without your custom classes) to get a separate output stream to write this image content onto the page,

              i dont make the Imageicon, just get the image Blob from the DB. then find its length.
              then when i do

              response.setContentType("image/gif");
              response.getOutputStream.write(image.getBytes(1,image.Length()));


              Tomcat webserver says, method .getWriter as already been called for this response object and displays an IllegalStateException and webBrowser shows the regular
              empty image placeholder.

              10:27:32,595 INFO [STDOUT] hi there
              10:27:32,615 INFO [STDOUT] filename jchatbox.gif
              10:27:32,625 INFO [STDOUT] imagecom.mysql.jdbc.Blob@85a863
              10:27:32,665 INFO [STDOUT] ilength=5779
              10:27:32,665 INFO [STDOUT] sucessflag
              10:27:32,675 INFO [STDOUT] GenericException: getWriter() has already been called for this response
              10:27:32,695 INFO [STDOUT] Caught an unexpected exception!
              10:27:32,695 INFO [STDOUT] java.lang.IllegalStateException: getWriter() has already been called for this response
              10:27:32,845 INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteResponse.getOutputStream(CoyoteResponse.java:568)
              10:27:32,865 INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteResponseFacade.getOutputStream(CoyoteResponseFacad
              ava:148)
              10:27:32,945 INFO [STDOUT] at dev.servlets.AccessTimerServlet.doPost(AccessTimerServlet.java:107)
              

              I understand this , coz i have already
              response.setContentType("text/html");
               PrintWriter out = response.getWriter();

              in my servlet code.
              so what do you think can be get-around for this situation.
              or if you can please provide some more insight into your own code i would really appreicate it...as i am nearing my MAster's project deadline...and need help with this ...pretty badly.
              thanks .
              robin

              • 4. Re: Image Retrieved from DB, doeesnt get displayed correctly
                Joan Horta  Tosas Novice

                Hi robinbajaj.
                I'm a little confused with the code snippet you provided... Is that servlet writing HTML an the icon in the same response? If so, I think that's not allowed, so you should provide a servlet for the HTML and another one for the images.
                If your servlet just provides an image, then you must not call "response.getWriter()" 'cause this method provides a character oriented output stream and you need a byte oriented one, which is provided with the call "response.getOutputWriter()". Finally, say that you cannot call "response.setContentType()" after writing something to the output.