Switch to full style
What's behind JSP & Servlets
Post a reply

Display images on jsp from servlet

Mon Jun 29, 2009 6:12 pm

Hello, I am peggie. Currently, I am trying how to display image from the database(mysqlquerybrowser) through the use of servlet.
However ,I do not know the codes to be put in jsp in order to call out the image from the servelet as well as the database.

Could you guys help me check my servlet and java class to see whether if there is any mistake.
As well as how to call out the images and display in jsp page:)

The code for my servlet is
Code:
package sg.nyp.edu.sit;
import java.sql.*;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class DisplayImage
*/
public class DisplayImage extends HttpServlet {
   private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public DisplayImage() {
        // TODO Auto-generated constructor stub
    }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      String connectionURL = "jdbc:mysql://localhost/images";
      java.sql.Connection con= null;
      try{
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         con = DriverManager.getConnection(connectionURL,"root","");
         Statement st1=con.createStatement();
         ResultSet rs1 = st1.executeQuery("select image from images where imageID ='2'");
         String imglen="";
         if(rs1.next()){
            imglen = rs1.getString(1);
            System.out.println(imglen.length());
            
}
         
         rs1 = st1.executeQuery("select image from images where imageID = '2'");
         if (rs1.next()){
            int len = imglen.length();
            byte[] rb = new byte [len];
            InputStream readImg = rs1.getBinaryStream(1);
            int index=readImg.read(rb,0,len);
            System.out.println("index" + index);
            st1.close();
            response.reset();
            response.setContentType("image/jpg");
            response.getOutputStream().write(rb,0,len);
            response.getOutputStream().flush();   
            
         }
         
      }
      
      catch (Exception e){
         e.printStackTrace();
         
         
         
      }
      
      
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
   }

}


The code for my java class is:
Code:
package sg.nyp.edu.sit.model;

public class Image {
   int imageID;
   Byte image;
   String imglen;
   public int getImageID() {
      return imageID;
   }
   public void setImageID(int imageID) {
      this.imageID = imageID;
   }
   public Byte getImage() {
      return image;
   }
   public void setImage(Byte image) {
      this.image = image;
   }
   public String getImglen() {
      return imglen;
   }
   public void setImglen(String imgLen) {
      this.imglen = imgLen;
   }
   
   
   
   
   }


My database in mysql query browser is called images
And the attributes are:
int imageID
Blob image
Thanks:D
Hope to receive u guys reply soon!:D



Re: Display images on jsp from servlet

Mon Jun 29, 2009 10:00 pm

I never tried try to use BLOb for image before , what i used is just to save image as file and use the id to create its file path and put it in <img > tag src .

Post a reply
  Related Posts  to : Display images on jsp from servlet
 Sequence of Images , draw several images in a row     -  
 how to run servlet and rmi     -  
 Servlet & JSP     -  
 JSP and Servlet     -  
 jsp and servlet     -  
 servlet coding     -  
 SERVLET/JSP DOUBT     -  
 Java Servlet and JSP     -  
 Servlet Help for my project!!     -  
 Here is how to display any 2d array     -