Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2009, 11:01 AM
Authorized User
 
Join Date: Jan 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unable to display out images from SQL Query Browser:(

Hello, I am peggie. Currently, I am using servlet to display out images on my image.jsp page. However, nothing appears.

This is my servlet code:
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
 }
}
This is my java getter and setter:
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;
}















}
Code:



This is my image.jsp

Code:
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="form1" method="get" action="./DisplayImage">


</form

</body>
Code:
</html>


My database is called: images
and the attributes are:
int imageID;
Blob image;

May I know what's wrong with it.
Thanks:D
Hope to receive ur reply soon!:D





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display sql server images in asp.net c# bobwith5 ASP.NET 2.0 Basics 0 October 20th, 2007 11:07 AM
Display text data in textarea from SQL query palvin Classic ASP Professional 13 November 30th, 2005 05:52 PM
Cannot display/return SQL Query Output from a Vari sjm SQL Server DTS 1 September 9th, 2005 11:40 AM
inserting images in ms sql server using query anal connect2sandep SQL Server 2000 2 July 24th, 2005 04:41 PM
Unable to create SQL Query using parameters odyzeus Crystal Reports 0 March 3rd, 2005 01:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.