Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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:

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:
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:
<%@ 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>
my database is called:
The attributes are:
pictureid (int)

Hope to receive u guys reply soon
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 03:14 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.