aspose file tools*
The moose likes JSP and the fly likes problem with server relative paths for images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "problem with server relative paths for images" Watch "problem with server relative paths for images" New topic
Author

problem with server relative paths for images

Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
Hi all,

I'm having trouble with server relative paths for my images, and hence the won't load in jsp's.

When I browse http://localhost:8080/myContext/images/images/logo.gif the image loads... so the files are there and can be served up. However, when I use a servlet to serve them up (as in http://localhost:8080/myContext/landing.s2s), the page does not show the images. I am able to right click on the image and it shows http://localhost/images/images/logo.gif (note that there is no port 8080 nor a context!)

My directory structure is:


I use tiles and sample portion of my layout.jsp is as follows:


One of the fragments that populates it (with images that won't load):



The image src= references seem OK to me.

Any thoughts on how I can get the references working? Is there a request.{method} that will give me "http://localhost:8080/mycontext"? The closest I can come is request.getServerName() + ":" + request.getSevletContext(), and I can't get this to work. Moreover, do I really need to do this? shouldn't a reference to the image directory under my WebContent directory be served up by the container? (I'm using Tomcat 5.5, Struts 1.3.10, Tiles -- this was ported from Resin, and it DID work fine there... maybe I fatfingered something?)

Any ideas? thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

The server-side markup is considerably less interesting than the client-side result would be.

And why are you using scriptlets rather than the EL to grab the context path?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
laziness, really. I don't know EL syntax as well. I'll try it-- but shouldn't it give the same results?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Yes, but it's really bad form.

So what do the base tag and the img tags look like once they hit the browser? Thats's what matters. The server-side markup is non-revealing.
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
using these <link> and <base> tags:


I get this source:

Css-es are fine, but the images don't load. Right click--> properties on IE gives: http://localhost:8080/images/images/logo.gif (note missing servlet context)
If I hardcode (not recommended, obviously) the <base> tag to:

gives me the following source:


but images still fail to load. Right click--> properties on IE gives: http://localhost:8080/images/images/logo.gif Browsing to http://localhost:8080/myContext/images/images/logo.gif brings up the image.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Is the context path myContext or Ship2Storage? Your post is very inconsistent on this matter.

And, yet again, what URLs are you using on the img tags?
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
Please use "myContext." I made the edit.

Thanks for checking this out. I appreciate it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

One last time:
Bear Bibeault wrote:And, yet again, what URLs are you using on the img tags?
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
And, yet again, what URLs are you using on the img tags?

I guess I don't understand what you mean-- are you asking how I've coded them in the JSP or how the browser actually displays them (as in right-click on the image, properties?) or something else...
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
i code them as follows:



If i change to

then the image shows up. I was hoping to use the Base tag so that I don't have to recode a mass of JSP's.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

So if the latter format works -- which is the only form that works with the base tag (the leading slash prevents the base tag from coming into play) -- why not use it?
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
Actually, I didn't realize the leading slash issue until last night when I posted that, and I'm still hoping to address this globally, rather than at the individual jsp page. I don't know the functionality of each page very well (I inherited the site from others who created it), and there is a ton of them each with numerous images. I was hoping to address via a setting or tag (like <BASE>).

But if that won't work, I think I'm left with using regex to change the leading slash on each JSP page or fragment. Maybe NotePad++ with its change across mutiple files is a good option.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

If you are going to touch each <img> tag URL, I'd probably do it unambigously with something like:
and not leave anything to chance.
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
An excellent suggestion. I'll try it on a page or two before I roll it out to the rest. Thanks for your help.
 
 
subject: problem with server relative paths for images