This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes displaying image in jsp from sql using helper servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "displaying image in jsp from sql using helper servlet" Watch "displaying image in jsp from sql using helper servlet" New topic
Author

displaying image in jsp from sql using helper servlet

ekansh singh
Greenhorn

Joined: Mar 05, 2009
Posts: 5
I am struck on a simple problem
I have to display an image from a database. I have successfully uploaded the image in database.
now for displaying image on JSP I have a servlet which is doing this work

This servlet is stored in src folder under package action. Actually I am using struts2 and i dont know how to display it using struts2.
So my servlet is like this


getImage functionality is like this


}
[/code]

On JSP my code is


on searching on net i found this also

i tried using both but I am unable to display the image




Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

When hitting the servlet directly in the browser displays the image, it will work from an <img> tag in an HTML page.

So work on your servlet -- make sure that it returns the right data and has the correct response headers set.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67


Why are you doing this?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
ekansh singh wrote:Actually I am using struts2 and i dont know how to display it using struts2.
This is irrelevant. Struts is just another MVC framework.

i tried using both but I am unable to display the image

Aside from a lot of logic faults and resource leaks I see two huge flaws: 1) you're calling super.doGet() in the servlet and 2) you're getting only the first 20000 bytes of an image and ignoring the rest in the DAO.

Check the 2nd example of this article how to do it properly: http://balusc.blogspot.com/2007/04/imageservlet.html
As a suggestion, first test the image independently in your webbrowser without using HTML <img> element.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying image in jsp from sql using helper servlet