File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Display Image in JSP retrieved from database by servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Display Image in JSP retrieved from database by servlet" Watch "Display Image in JSP retrieved from database by servlet" New topic

Display Image in JSP retrieved from database by servlet

Anam Zahra

Joined: Jan 12, 2009
Posts: 11
Hi... I have a servlet that is getting an image from the database. After getting image I want to display it in a JSP page. But web page is not displaying the image. Servlet is working fine in my opinion, because when I run the servlet it shows me dialog box to save the image and I am able to save the image successfully. Please help me asap. Thanxs
............This is the servlet............

This is img tag by which I am trying to display the image

<img src="/Servlet/DBImage" alt="My image" height="200px" width="200px">
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13025
HTML IMG tag requests always use the GET method - why did you use doPost?

What is the idea behind these statements?

Rob Spoor

Joined: Oct 27, 2005
Posts: 20184

1) You should write only the image contents, nothing else (so no HTML, HEAD or BODY tags).
2) You should not read a binary image as a String. Binary data can usually not be displayed in Strings.
3) You should never ever ever ever trust anything that comes from the user. Use PreparedStatement to prevent SQL injection.
4) Your way of writing is flawed.[], int, int) doesn't necessarily need to read as much as you want. You should use a loop for reading and writing.

How To Ask Questions How To Answer Questions
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196

Hi, Where are you passing the request parameter named "value". The link img src="/Servlet/DBImage does not seem to pass any request parameter to your Servlet.
subject: Display Image in JSP retrieved from database by servlet
It's not a secret anymore!