Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

display image from database on jsp page

 
Ricardo D'Angelo
Greenhorn
Posts: 1
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.
 
altanai bisht
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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();
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Diogo Moreira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you are referecing the image in the .jsp page?

 
Stefan Evans
Bartender
Posts: 1691
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you certain you actually have an image saved into that database field?
How did it get there?


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic