• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dispatching from Servlet to html file, images not found

 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic