aspose file tools*
The moose likes JSP and the fly likes JSP + Images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP + Images" Watch "JSP + Images" New topic
Author

JSP + Images

Niall Loughnane
Ranch Hand

Joined: Dec 07, 2006
Posts: 208
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

Joined: Jun 20, 2008
Posts: 15
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

Joined: Jan 10, 2002
Posts: 61654
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

P.S> If your web app is the root context, the context path will be the empty string.
mark walter
Greenhorn

Joined: Jun 20, 2008
Posts: 15
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

Joined: Jan 10, 2002
Posts: 61654
    
  67

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.
 
 
subject: JSP + Images