• 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

how to display images on JSP in struts2

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads 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