• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Putting an image on JSP

 
Luciano A. Pozzo
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • 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
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I actually meant response.getOutputStream(), and then use ImageIO...

Tx for pointing it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic