Total members 11366 |It is currently Fri Oct 24, 2014 9:27 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R

All times are UTC [ DST ]



  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


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
  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 .

_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 time
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 Here is how to display any 2d array     -  
 display list     -  
 SERVLET/JSP DOUBT     -  
 Java Servlet and JSP     -  

All times are UTC [ DST ]









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team