• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Display image in a JSP that retrieved from a database?

 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it able to display image in a JSP that persisted as a LOB to a database?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP creates HTML.

So how do you display an image in HTML?
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy it just have to do give path to the image file in <img src="path". But when that image taking from a database that solution can't be use. in's it?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not? The src attribute of the image tag identifies a server-side resource that provides the image data. That can be anything you want it to be -- it doesn't have to be a static file.

Hint: perhaps a servlet?
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really are you sure that image will display if use <img scr"servlet?imageId=22" with other textual HTMl things. Can you please explain.

my JSP page structure

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Udara Amarasinghe wrote:Really are you sure that image will display if use <img scr"servlet?imageId=22" with other textual HTMl things.

Of course not. You can't return more than one response to a single request. The servlet would serve only the image. Why would it need to serve HTML too?



As I already explained. In the JSP you use an image tag to reference a servlet that returns the image data.

And you are still using Java scriptlets in JSPs? In 2011? And even so, the use of out.println() has never been proper practice. If you are still going to use scriptlets (even though they've been discredited for almost 10 years now), you would use scriptlet expressions.
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable advice.
But I have still not found a solution to display both image and html text
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again: the JSP provides the HTML text just like any other HTML page. The image tag provides the image just like any other HTML page.

Why are you hung up on trying to provide them together? That's not the way HTML works.
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I need to create user profile page that page should include his details and his photograph. mmmmm
I think I will not be able to produce that. Thank you very much for spend your time for my post.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you can produce that. Why not? It's simple HTML!

Example:

Why all the confusion?

 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Of course you can produce that. Why not? It's simple HTML!

Example:

Why all the confusion?



That solution not gives what I want. So I decide to try new technique that is store image in file system with user id as the name of image file and add path for image in <img scr="path"+<%=user.userId() %>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does it not "give you what you want"? And there is no difference between the HTML for grabbing the image from the file system. or grabbing it from a servlet. None.

In your example (which is full of errors and which cannot work as shown): <img scr="path"+<%=user.userId() %>

If you replace the value of the src attribute with a servlet URL, it will work just the same.

Why are you resisting something so simple?
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK so what kind of servlet are you suggest to use here. Is it a servlet that write byte array of the image using resonse.getOutputStream().write(bytearray); or a servlet that return the image location.... I don't know.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet should stream the image data as the response and set the response headers appropriately. There are plenty of examples in this forum and on the web.
 
Rajat Panda
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Friend,
Use <iFrame ....>. This helps image/pdf/.txt display on JSP page.
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can do that...

First write a method which retrieves the data from blob and saves the image as ".jpeg" format at server side and return the name of the created image file.

Now after you receive the file name from the method, you can give this as relative path to the <img> tag .

NOTE: Need to create a script to remove all the images which were created by above method once every week to improve the server memory.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat Panda wrote:Use <iFrame ....>. This helps image/pdf/.txt display on JSP page.

An iframe? How on Earth would that help?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Smarty Ravi wrote:First write a method which retrieves the data from blob and saves the image as ".jpeg" format ...
That is really inefficient and needless. The image can be streamed directly from the DB (I assume that there's a good reason that the image is on a DB, otherwise, it'd already be in a file).
 
Ravi Majety
Ranch Hand
Posts: 59
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: That is really inefficient and needless. The image can be streamed directly from the DB (I assume that there's a good reason that the image is on a DB, otherwise, it'd already be in a file).


may be my way would be inefficient but this also forms a way to perform. Let me know if there is a solution for it. I am would be very happy to learn new things and efficient ways...

Thank you

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already answered. Have a servlet that reads the image info from the DB and streams it as the response. The URL of that servlet is used by the image tag.
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Bear Bibeault if you please can show the source code it will be very cool.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Udara Amarasinghe wrote:Mr. Bear Bibeault if you please can show the source code it will be very cool.

You know that's not the way things work here. But if you google, you'll find lots of example code.

Also, a CodeRanch staffer has provided this example which streams from a file, but can be adapted to read from a DB.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!