Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP + Images

 
Niall Loughnane
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • 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
 
mark walter
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic