This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Displaying image and text together from database

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kshitiz Agarwal
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Click the disable HTML checkbox when posting.
 
Bear Bibeault
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to prefix the url to the image with the context path. Use: ${pageContext.request.contextPath}
 
Kshitiz Agarwal
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That URL does not match the pattern you specified in the deployment descriptor.
 
Kshitiz Agarwal
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:thumbup:

Now work on stopping using scriptlets in your JSPs!
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic