• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JSP + Images

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

any info on this would be cool:

i'm trying to create a JSP page and display an image that is stored on the file server and contained within the war file...there are numerous "image" directories within the application

but when i do <img src="<% request.getContextPath(); %>/images/example.jpg"> from within the jsp page the image isn't displayed

the parameter request.getContextPath displays nothing when i check it...so how i can set the contextPath also?

Cheers,

Niall
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Niall, If these images are contained within the war file then they are static images. So why do you need to build a dynamic url?
<img src="<path>/images/some.jpg"/> should work just fine.
 
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mark walter:
Niall, If these images are contained within the war file then they are static images. So why do you need to build a dynamic url?
<img src="<path>/images/some.jpg"/> should work just fine.

Becasue hard-coding the context path is an egregious violation of best practices.

Look at your code very carefully:

<img src="<% request.getContextPath(); %>/images/example.jpg">


Do you see the error?

Compare it to the code in the JSP FAQ on this issue.
[ August 05, 2008: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S> If your web app is the root context, the context path will be the empty string.
 
mark walter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
P.S> If your web app is the root context, the context path will be the empty string.



Hmm, doesnt look like I said hard-coded anywhere. Guess I should have said web app like your above comment to be specific.
 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just reread your post and it still sounds to me like you were advocating hard-coding the context path. Glad to hear that you were not.
 
Popeye has his spinach. I have this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic