Total members 11889 |It is currently Mon Mar 18, 2024 10:25 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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

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 .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 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     -  
 JSP and Servlet     -  
 jsp and servlet     -  
 how to run servlet and rmi     -  
 Servlet & JSP     -  
 Java Servlet and JSP     -  
 Servlet Help for my project!!     -  
 servlet coding     -  
 SERVLET/JSP DOUBT     -  
 SEVEN SEGMENT DISPLAY     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com