This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display Image in JSP retrieved from database by servlet

 
Anam Zahra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTML IMG tag requests always use the GET method - why did you use doPost?

What is the idea behind these statements?

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic