• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

display image from database on jsp page

 
Greenhorn
Posts: 1
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's one way to do it using swings ( works even though deprecated )

Class.forName("org.postgresql.Driver");
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(rs.next()) {
String name = rs.getString(1);
System.out.println(name);

InputStream is = rs.getBinaryStream(2);
System.out.println(is);
final Image img= ImageIO.read(is);
System.out.println(img);

JFrame frame = new JFrame("Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(img.getWidth(frame), img.getHeight(frame));
frame.add(new JPanel() {
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
});
//frame.show();
frame.setVisible(true);

}
rs.close();
}
ps.close();
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How you are referecing the image in the .jsp page?

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you certain you actually have an image saved into that database field?
How did it get there?


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic