*
The moose likes JSP and the fly likes Displaying image and text together from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying image and text together from database" Watch "Displaying image and text together from database" New topic
Author

Displaying image and text together from database

Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Hi,

I am trying to display image and its respective name from the database. The code is:



The above code is showing name but image is not getting displayed. If I use output stream (as commented), then name is not displayed. WHat should I do to make both of them appear?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

The URL for the <img> tag needs to be to a servlet that sends the image data. Having the image blob in the JSP is not useful. Remember, JSP just creates HTML. When you write the page in HTML do you give the image tag a blob of data? No. You give it the URL of the image.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Bear Bibeault wrote:The URL for the <img> tag needs to be to a servlet that sends the image data. Having the image blob in the JSP is not useful. Remember, JSP just creates HTML. When you write the page in HTML do you give the image tag a blob of data? No. You give it the URL of the image.


Hi,

I am trying to do the same as suggested by you...:

This is my servlet:



And i am calling it from jsp like this



with mapping in web.xml as




But even servlet is not being called...what i am doing wrong?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

ksh agarwal wrote:And i am calling it from jsp like this

<img />


Be sure to disable HTML when you post HTML code or your image tags will be empty.

Please repost with the real code.
Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Bear Bibeault wrote:
ksh agarwal wrote:And i am calling it from jsp like this

<img />


Be sure to disable HTML when you post HTML code or your image tags will be empty.

Please repost with the real code.


Sorry...my mistake...the code is



PLease consider them under tags <> as I am not able to display those with tags...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

ksh agarwal wrote: as I am not able to display those with tags...

Click the disable HTML checkbox when posting.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

You need to prefix the url to the image with the context path. Use: ${pageContext.request.contextPath}
Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Bear Bibeault wrote:
ksh agarwal wrote:And i am calling it from jsp like this

<img />


Be sure to disable HTML when you post HTML code or your image tags will be empty.

Please repost with the real code.


Hi,

The image tag is
Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Bear Bibeault wrote:You need to prefix the url to the image with the context path. Use: ${pageContext.request.contextPath}


Hi,

You mean to say this...




But even then servlet is not being called. Its first line which is sysout is not getting executed. I have seen examples and people are actually using this approach only.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

That URL does not match the pattern you specified in the deployment descriptor.
Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Bear Bibeault wrote:That URL does not match the pattern you specified in the deployment descriptor.


Hi,

I have changed the mapping to



Servlet was not being called unitl I changed from handleRequest to doGet in servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Are you setting the content headers on the response correctly? Is the image data correct?

Likely easier to debug if you just type the URL for the image into the browser's address bar.

Use the browser tools to inspect the response for correctness.
Kshitiz Agarwal
Ranch Hand

Joined: Dec 15, 2011
Posts: 36
Bear Bibeault wrote:Are you setting the content headers on the response correctly? Is the image data correct?

Likely easier to debug if you just type the URL for the image into the browser's address bar.

Use the browser tools to inspect the response for correctness.


Hi,

I am able to show image now. Actually parameters were not getting passed properly in the request. Now its all done....thank you very much for your help and time :)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

:thumbup:

Now work on stopping using scriptlets in your JSPs!
 
jQuery in Action, 2nd edition
 
subject: Displaying image and text together from database
 
Similar Threads
Generating an Image in Custom Tag
how to retrieve image in jsp page from a seperate folder.
image is not uploaded into the database
Problem in uploading image to to mysql database
Display values form database