aspose file tools*
The moose likes JSP and the fly likes Putting an image on JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Putting an image on JSP" Watch "Putting an image on JSP" New topic
Author

Putting an image on JSP

Luciano A. Pozzo
Ranch Hand

Joined: Jun 20, 2005
Posts: 112
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Jul 19, 2005
Posts: 99
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.


We're doomed!!<br />Yay!!!<br />No that's bad Girr!!<br />Yay!!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Scheepers de Bruin
Ranch Hand

Joined: Jul 19, 2005
Posts: 99
I actually meant response.getOutputStream(), and then use ImageIO...

Tx for pointing it out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Putting an image on JSP