wood burning stoves 2.0*
The moose likes JSP and the fly likes Why won't my images display right in JSP under tomcat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Why won Watch "Why won New topic
Author

Why won't my images display right in JSP under tomcat?

Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Hi chaps,
I am having a pretty serious problem with my web application - the stupid thing won't show images or attach stylesheets. Its like they are linked wrong.
I'm wondering if when you use tomcat and other servlet engines, is there something special you have to do with the image folder or something?
See, I'm pretty sure there isn't anything like that. I've put my images in a folder called images like:
webapps/mywebapp/images
and the actual jsp pages in:
webapps/mywebapps
The jsp pages show up and everything put anything linked is poo pooed.
The thing that's really bugging me though is the fact that when I use the preview page feature in Dreamweaver, the images are shown fine. I've compared the source code via IE and Opera and the code is identical - in fact everything is identical apart from th actual name of the jsp page, because Dreamweaver uses a random name to preview pages
Please, if anyone can help with this I would be very greatful. I'm not sure if its a tomcat/container or maybe even a dreamweaver problem, but its happend every time I've tried to make a webapp so I'm about to go nuts

Thanks to anyone who can offer some suggestions
Kind Regards
Simon
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
What do the <img src="..."> urls look like on your JSP page sources?
What do they look like when Tomcat serves your pages?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
I count the same problem some times ago.Dreamweaver using a temp file(like 001454654sdsname.jsp) to brows.To disable this function you will do this:
in Dreamweave toolbar:Edit->parameter selection---->brows in browser->option--->using temp file brows.You will not check out the:"using temp file to brows" option.


When I face C# and Java I choose Java.
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Hi Guys,
Well, you were spot on there Ron. There was a difference between the two.
The actual code for the page is:
/images/darkblue_gradient.gif
However, if I look at the properties of the image once it's been served I get:
http://localhost:8080/images/darkblue_gradient.gif
BUT what I need it to be is:
http://localhost:8080/qds/images/darkblue_gradient.gif
Notice the qds web application directory.
Do either of you know how i can use relative addressing using Dreamweaver.
I have told Dreamweaver that my default images folder is C:/webapps/qds/images so I'm not sure whats going on here. Of course that default folder is for my local copy
Can you let me know how to set up the addressing for images. That would be so greatly appreciated, I may not even through my monitor out of the window at that stupid dog outside thats constantly all hours day and night. Yes, I think I'll use a the old hand grenade instead. That way i might take out the dogs idiot inbred owners.
Ahem. Sorry. I got a bit sidetracked there!
Many thanks for your help chaps
Simon
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Try changing it from
img src="/images/darkblue_gradient.gif"
to
img src="images/darkblue_gradient.gif"
that is, take off the "/" at the beginning of the relative URL.
If you're using JSTL, the <c:url> tag might be a better approach.
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Ron you are a life saver.
Just one last question then:
Whats the difference between putting a / at the very front of a relative address. Me not get it!
Thanks mate, youve been a huge help
Simon
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Actually guys, I spoke to soon.
The problem is essentially fixed, but there is one irritating problem. When I add an image using dreamweaver, it automatically puts a / in front of the address. This of course is exactly whats screwing things up here.
Does anyone know how I can get round this. I'm certain there must be a way. It seems like a pretty silly problem
Thanks again chaps
Simon
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Hi
Sorry, im back. But with good news this time:
I summoned all of my worldly wisdom and managed to fic the problem. I just needed to set an option saying link relative to the doc rather than the root.
Although, I don't know why the root option wouldnt work. Why wouldn't te images folder be addressable from the root of the app?
I'm also a bit worried about how linking might work when my pages get further and further down the file system. I just have a nasty feeling that something will go wrong
Anyway, things are sort of working now even if I dont understand why they weren't before
Thanks
Simon
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

I use getServletContext().getRealPath("dirname/fileName") when I need to manipulate files within my web applications.


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
I like to use the very handy "base" HTML tag - inserted in the "head" area of a page it tells the browser a base URL to request resources from.
Works for CSS, image, etc. resources.
Bill
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I don't know anything about Dreamweaver. If it's doing the wrong thing, don't use it.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
If the relative address begins with "/", the browser interprets it to mean "relative to the root of the server" -- the whole server, not just your webapp.
If the relative address doesn't begin with "/", the browser interprets it to mean "relative to the URL of this page".
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I use getServletContext().getRealPath("dirname/fileName") when I need to manipulate files within my web applications.

But in this case, the web app isn't manipulating a file -- it just needs to generate a valid relative URL that the browser will interpret correctly.
[ July 19, 2003: Message edited by: Ron Newman ]
Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Thank guys!

Simon
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why won't my images display right in JSP under tomcat?
 
Similar Threads
Getting null value from session in Tomcat public_html
Using Tomcat3.2.1 / 4.0.1 in Stand-alone
eclipse and birt.
Applet is not getting loaded in client machine
Converting HTML to an image?