• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying Image in JSP

 
Kapil Shardha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to display an image that is stored in database, on a JSP page. The image gets displayed on whole page but I want to display it in a small area as the JSP page has other elements too.Following is an excerpt from my JSP code, where "image" is the byte array having bytes of image extracted from database.




Kindly suggest me solution for this problem.

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what you are doing.

JSP is merely a templating mechanism for creating HTML pages to send to the browser. Once the response gets to the browser it's just an HTML page like any other. The browser has no knowledge of whether the page came from a JSP, or if you hand-coded it, and treats the page the same as any other HTML page.

So if you were writing this HTML page yourself, would you put the raw data of the image in the src attribute of the <img> tag? Really?

How would you write the tag? Whichever way you would do it by hand is the same way your JSP needs to do it.

So with that in mind, how do you solve your problem?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[For your information]why are you save an image into a database? in other hand you can save your image location as a varchar into your database[i prefare this one]
 
Kapil Shardha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understood the reason behind not using image tag in this case but I am still not able to figure out how to render this data on html/jsp page. Kindly suggest me how to solve this issue.

P.S I am saving images in database as a part of requirement of application.


Thanks


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kapil Shardha wrote:I understood the reason behind not using image tag in this case

Who said anything about not using the <img> tag? Of course you use the <img> tag.

Again, the same way that you'd use it in an HTML page.

So answer my question: if you were hand-coding this in HTML, how would specify it?
 
Kapil Shardha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HTML, the src attribute of img tag takes a path of an image residing somewhere in memory (correct me if I am wrong).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kapil Shardha wrote:In HTML, the src attribute of img tag takes a path of an image ...


Right. It doesn't contain image bytes. So your JSP needs to do the same. Specify a path, not bytes.

Secondly, the src attribute specifies the URL of a server resource that returns the image data no matter how that data is stored.

The latter part is important. As far as the browser is concerned, as long as the response to the src URL serves the image data, it doesn't care where it comes from.

So now what do you do next?
 
Kapil Shardha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I was able to display image in jsp page. Your suggestions helped me.

Thanks

Kapil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic