aspose file tools*
The moose likes Servlets and the fly likes Dispatching from Servlet to html file, images not found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dispatching from Servlet to html file, images not found" Watch "Dispatching from Servlet to html file, images not found" New topic
Author

Dispatching from Servlet to html file, images not found

William Stafford
Ranch Hand

Joined: Dec 13, 2004
Posts: 109
I'm trying to dispatch from a servlet to an html file. The file displays but the images in the html file are not found. If I invoke the html file manually the images do appear. I'm guessing this is some sort of "path relative to what..." problem but I'm stumped.

Sample code:
// map1.htm contains an <img src="map1.jpg"> tag.
// map1.jpg is in the same directory as map1.htm
RequestDispatcher rd=servletContext.getRequestDispatcher("/maps/map1.htm");
rd.forward(req, res);
// no images found

http://localhost:8080/maps/map1.htm
// images found
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

You are probably using page-relative addressing for your images. No worky!

Use server-relative addressing. Such as:

/contextPath/images/foo.jpg

Search this forum for info on how to dynamically determine the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Stafford
Ranch Hand

Joined: Dec 13, 2004
Posts: 109
Bear,

Thanks for the rapid reply. Unfortunately, I can not change the image source paths in the image tags. The html pages are also accessed directly by existing portions of the application. I have to use the existing convention of image tags where the src is the same directory as the including html file.
I understand what you suggested but I need a way to invoke the html page so the the "current dir" is the directory containing the html page. When I dispatch from the servlet I think the current directory is the servlets directory not the html files directory. Is this correct?

Is there another way to pass control the an html file from a servlet?

Thanks,
-=beeky
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

Originally posted by William Stafford:
When I dispatch from the servlet I think the current directory is the servlets directory not the html files directory. Is this correct?


Not quite. There is no such conept as "current directory" with a servlet. But the browser doesn't know that and will issue a request using the servlet path as if it were a physical file path. Hence, no images.

Is there another way to pass control the an html file from a servlet?


If you redirect (rather than forward), the new request will have the right path for the browser to resolve the image paths. But that'll probablky defeat the purpose of using a servlet in the first place, no?

Otherwise, you'll either need to change the src values, add a <base> tag to the HTML pages, or set up servlet mappings for the images so that they can be found by the server using the bogus browser-generated URLs.
[ July 25, 2007: Message edited by: Bear Bibeault ]
William Stafford
Ranch Hand

Joined: Dec 13, 2004
Posts: 109
Bear,
Thanks for the thorough explanation. I believe I will have to use redirect even though, as you pointed out, that makes the Servlet sort of functionless. What I did not mention was that this functionality is a place holder. Eventually the servlet will be doing something useful. In the mean time all it needs to do is show one of the map files.
Thanks again for taking the time to explain it all.

-=beeky
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Another workaround would be to create a servlet mapping that makes the servlet appear to the browser to be in the same directory as the HTML pages.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dispatching from Servlet to html file, images not found
 
Similar Threads
Could someone try and explain
where to store the .htm files related to the servlet
File Permission Error
where to put war
images not loading