• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Putting an image on JSP

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I put an image from database (byte[]) on a jsp page? If somedoby have the surce code please post.

PS: I'm using struts, I found the tag <html:img> but I need to show the image without write an .jpg file on my hard-disk, like dymanic images.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please remember that every image on a web page is the result of a separate request to the web server. You can't mix the text of a web page with the binary data of an image. There have been MANY discussions in this forum - you should use a servlet to handle requests for binary data.
Bill
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can point you in the direction of one solution:

<img src="http://myapp/.getimage?img_code=123">

Create a servlet and map "*.getimage" to it in the web.xml file of your web application. Because struts normally looks for "*.do" it should not interfere with struts:

<servlet>
<servlet-name>getImage</servlet-name>
<servlet-class>com.myapp.GetImage</servlet-class><!-- the fully qualified classname of your servlet -->
</servlet>
<servlet-mapping>
<servlet-name>getImage</servlet-name>
<url-pattern>*.getimage</url-pattern>
</servlet-mapping>

in the doPost(..) method of your servlet, set the content type (where you would normally use "text/html") to "image/gif":


public class GetImage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("image/gif");
PrintWriter out = response.getWriter();

/* read your image from the database, and use the PrintWriter to
write it to the response
*/
out.close();
}

}

You can find a list of subtypes here for your specific format.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scheepers de Bruin:

/* read your image from the database, and use the PrintWriter to
write it to the response
*/



Did you mean "use ServletOutputStream to write it to the response"?

PrintWriter is for text.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletOutputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintWriter.html
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually meant response.getOutputStream(), and then use ImageIO...

Tx for pointing it out.
 
You showed up just in time for the waffles! And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic