aspose file tools*
The moose likes JSP and the fly likes Displaying Image in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying Image in JSP" Watch "Displaying Image in JSP" New topic
Author

Displaying Image in JSP

Kapil Shardha
Greenhorn

Joined: Jun 18, 2009
Posts: 9
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


SCJP 6 (83%)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

[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

Joined: Jun 18, 2009
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Jun 18, 2009
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61224
    
  66

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

Joined: Jun 18, 2009
Posts: 9
Hey, I was able to display image in jsp page. Your suggestions helped me.

Thanks

Kapil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying Image in JSP