my dog learned polymorphism*
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
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
Author

Display Image in JSP retrieved from database by servlet

Anam Zahra
Greenhorn

Joined: Jan 12, 2009
Posts: 5
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
Rancher

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

What is the idea behind these statements?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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. InputStream.read(byte[], int, int) doesn't necessarily need to read as much as you want. You should use a loop for reading and writing.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display Image in JSP retrieved from database by servlet