File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes display image from database on jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "display image from database on jsp page" Watch "display image from database on jsp page" New topic

display image from database on jsp page

Ricardo D'Angelo

Joined: Jun 20, 2012
Posts: 1

Hello there, this is my first post on this forum (and what a post it is!)
I am writing about how to display an image stored in a postgresql database as a bytea type, on a java/jsp webpage. I am developing on Linux Karmic Koala, using Eclipse editor with Tomcat plugin, Tomcat 5.5 and Firefox browser. So far I've tried three methods, one using a servlet, one running the main method on the Eclipse command console, and the other calling a regular java class method from a jsp page. So far all three have been unsuccessful.

The application I'm developing for my company is just a dating website, the application name is "lovestory" and it's located on "/home/ricardo/workspace/lovestory/" on my laptop, and I'm developing with java/jsp.

Here's what I've done so far:

1. Servlet method

When I call the above servlet (called GetImg) from Firefox, I get a blank page with the text "http://localhost:8080/lovestory/GetImg" written on top, which is exactly what I entered on the browser to call the servlet, and no error messages. By the way, the text does not behave like text, I cannot highlight it, behaves more like an image!

2. main method

When I run main on the Eclipse command console, I also get no error messages, but the image file that was created does not display either on my image viewer program which says the file is actually not a jpeg file, nor on Firefox browser which actually displays a blank page with the file path "file:///home/ricardo/Desktop/testimage.jpg" displayed on top (again behaving like an image instead of text).

3. getPicture() method

section of webpage (http://localhost:8080/lovestory/henkou1.jsp) where I call getPicture():

Finally, when I call getPicture() from henkou1.jsp, I get about the same result as the above two methods: the text "http://localhost:8080/lovestory/henkou1.jsp" which is what I entered on the browser, written on top of a blank page and behaving like an image again. This time I also get the following error message: "java.lang.IllegalStateException: getOutputStream() has already been called for this response"

This is my first experience trying to display images from a database in raw byte format, any help will be much appreciated.
altanai bisht

Joined: Aug 20, 2012
Posts: 2
here's one way to do it using swings ( works even though deprecated )

System.out.println("driver class found");
conn = DriverManager.getConnection(ur, username, password);
System.out.println("connection "+ conn);

PreparedStatement ps = conn.prepareStatement("SELECT * FROM tablename");
ResultSet rs = ps.executeQuery();
if (rs != null) {
while( {
String name = rs.getString(1);

InputStream is = rs.getBinaryStream(2);
final Image img=;

JFrame frame = new JFrame("Image");
frame.setSize(img.getWidth(frame), img.getHeight(frame));
frame.add(new JPanel() {
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

altanai bisht wrote:here's one way to do it using swings ( works even though deprecated )

As Swing (not "swings") cannot be used on a JPS, this isn;t at all useful.

[Asking smart questions] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

WHy do you have a main method in a web app?

In any case, you need to use an <img> tag to show an image. The URL should reference the servlet that serves the image data.
Diogo Moreira

Joined: Aug 13, 2012
Posts: 2
How you are referecing the image in the .jsp page?

Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
Are you certain you actually have an image saved into that database field?
How did it get there?

I agree. Here's the link:
subject: display image from database on jsp page
It's not a secret anymore!