aspose file tools*
The moose likes Struts and the fly likes how to display images on JSP in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to display images on JSP in struts2" Watch "how to display images on JSP in struts2" New topic
Author

how to display images on JSP in struts2

Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
i have written the following code in Struts2 for storing in BLOB datatype in MSQL.


following is the query.


Could someone please help me to display these images on a JSP.


SCJP 1.5
www.licexpadvice.com
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, your code makes absolutely no sense if you're trying to store the image as a blob--the only code I see copies a file from one place to another, and another one sets some string data for the same row.

In any case, if you're actually storing the image to the DB, you'd need to get those bytes and stream them to the client; searching the web for "display image from database" or something will provide a lot of solutions. Personally, I'm not a big fan of storing image data in a database, and generally store images on the file system. You'll still need to stream them to the client, though, with an image servlet, or in Struts 2, a "stream" result.
Hrishikesh Maluskar
Ranch Hand

Joined: Jun 19, 2008
Posts: 115
i have googled for tutorials regarding images but was not able to find one .....somebody please help me with finding the tutorials...or tell me the name of a good Struts2 book where information regarding images is specified.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What specifically do you want to know?

http://www.google.com/search?q=show+image+from+database+%2Bjava
http://www.google.com/search?q=image+servlet

These returned hundreds of hits for me.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I think what the OP is missing is the basic structure of an HTML page when images are to be displayed. I really think this needs to become an FAQ on the Ranch.

The view page (JSP or whatever) is going to create an HTML page to be displayed on the client (browser).

When the user performs the action that results in this response, the JSP page will produce an HTML page which includes contents which the application wants to show. For example, a page title, page header, page content, page footer, etc.

If you want the page content to include an image, the JSP doesn't just spit out the image content. Instead, the HTML content sent by the JSP page includes an <img> HTML tag.

When the image content is stored in the database (or even just on the server machine somewhere), you can create an Image Servlet to serve it. Let's say you create an Image Servlet named "MyImageServlet" and based on your servlet mappings, it is accessible via the following URL:
http://localhost/myApp/MyImageServlet

Then you can build your <img> tag as follows:
<img src="http://localhost/myApp/MyImageServlet?imageKey=myImage123 />

In your image servlet, you would get the request parameter named imageKey and use the value to look up the image in the database (or file system, whatever).

Now, as far as how to send the image back to the client, just reference the numerous examples available when you Google for image servlet (rather than duplicating that here).

As you can see, the image content is not sent as part of the original JSP response. The HTML content sent to the client by the JSP processing includes an HTML <img> tag which results in a separate request to the server.

I hope this helps,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to display images on JSP in struts2