Total members 9952 | Gratitudes |It is currently Sat Feb 11, 2012 1:38 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: Display images on jsp from servlet
PostPosted: Mon Jun 29, 2009 6:12 pm 
Offline
Newbie
User avatar

Joined: Mon Jun 29, 2009 5:56 pm
Posts: 4
Has thanked: 0 time
Have thanks: 0 time

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


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Display images on jsp from servlet
PostPosted: Mon Jun 29, 2009 10:00 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 57 time
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 .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Need to Display Content using JavaScript in HTML page  Scripting Language  stankov  0
 database contents display in table format in swing  GUI  subho07  4
 aligning the scanned images  Java  Anonymous  0
 How to delete/remove images from excel 2007 using POI.  Java  vikash786  0
 Servlet & JSP  Servlets / JSP  Anonymous  1

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






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