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 July 1st, 2009, 01:47 AM
Authorized User
 
Join Date: Jan 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Unable to display images from servlets into jsp

Hello, I am peggie. Currently, I am using eclipse with Tomcat 6.0.18. And my database is created using mysql query browser:)

However the problem is in my jsp page it display a default box that have no picture that I had inserted to the datbase on it.

My code for creating this are:

Code:
package sg.nyp.edu.sit; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;
import java.io.IOException; import java.io.InputStream;
import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/** * Servlet implementation class DisplayBlobExample */ public class DisplayBlobExample extends HttpServlet { private static final long serialVersionUID = 1L;
/** * @see HttpServlet#HttpServlet() */ public DisplayBlobExample() { super(); // 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 Blob image = null; Connection con = null; Statement stm = null; ResultSet rs = null; ServletOutputStream out = response.getOutputStream(); try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/images"); stm = con.createStatement(); rs = stm.executeQuery("select image from picture where pictureid = '2'"); if (rs.next()){ image = rs.getBlob(1); } else{ response.setContentType("TEXT/HTML"); out.println("<html><head><title>Display out the blob image</title></head>"); out.println("<body><h4><font color = 'red'>Image not founde for the given id</font></h4></body></html>"); return; } response.setContentType("http://p2p.wrox.com/images/jpg"); InputStream in = image.getBinaryStream(); int length = (int) image.length(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } in.close(); out.flush();
} catch (Exception e){ response.setContentType("TEXT/HTML"); out.println("<html><head><title>Unable To Display image</title></head>"); out.println("<body><h4><font color='red'>Image Display Error=" + e.getMessage() + "</font></h4></body></html>"); return;
} finally { try{ rs.close(); stm.close(); con.close(); } catch (SQLException 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 }
}
My java class for getter and setter are:
Code:
package sg.nyp.edu.sit.model;
import java.sql.Blob;
public class Image { int pictureid; Blob image; public int getPictureid() { return pictureid; } public void setPictureid(int pictureid) { this.pictureid = pictureid; } public Blob getImage() { return image; } public void setImage(Blob image) { this.image = image; }
}
And my jsp page is for calling out the image from servlet is:
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 method="get"> <img src="/DisplayBlobExample"/> </form>
</body>
</html>
my database is called:
picture.
The attributes are:
pictureid (int)
image(blob)

Thanks:D
Hope to receive u guys reply soon





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to display out images from SQL Query Browser:( Peggie1990 Servlets 0 June 29th, 2009 11:01 AM
displaying images in jsp using struts vijayalexander JSP Basics 0 November 6th, 2006 09:46 AM
Unable to display the database hayley Classic ASP Databases 2 December 17th, 2004 03:32 AM
URGENT - Unable to compile class for jsp Black Balloon Apache Tomcat 6 August 27th, 2004 06:38 PM
Good Project in JDBC,JSP,Servlets,Java Beans Pooja J2EE 3 August 26th, 2004 08:05 AM





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