File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying image and text together from database

 
Kshitiz Agarwal
Ranch Hand
Posts: 36
  • 0
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 0
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:thumbup:

Now work on stopping using scriptlets in your JSPs!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic