I'm looking for more clever solutions.
My problem also occurs when I use RequestDispatcher.forward() to forward to a JSP page
(see original posting below). In my case, the JSP page contains relative links like
<img src="images/picture.gif"> and they are not being found. The JSP is not in the
server's root directory, but in a subdirectory called /myfiles (picture.gif is in a
directory called /myfiles/images).
I know I can solve the problem by using <img src="/myfiles/images/picture.gif">, but I
don't want to hard-code any of this path information into the JSP. I want to keep the
links relative to the JSP's location.
Since my servlet that does the forwarding knows the path, I've thought about storing a
value in the session object (i.e., session.putValue( "Path", "/myfiles" ). Then, I
could use <img src="<%= session.getValue("Path") %>/images/picture.gif">. But my
concern here is that I don't want to muck up the HTML tags and potentially draw the
wrath of web page designers who may be frightened by such a thing. Not to mention
they may be using GUI tools won't ever see the HTML.
I can also make it work by using sendRedirect() instead of forward(), but of course, I
don't want the extra trip to the client.
Is there a way to keep my relative links and still use forward()?
Dave F.
---------- Original Message ----------------------------------
From: Leo Neumeyer <[EMAIL PROTECTED]>
Subject: Re: getRequestDispatcher(aURL).forward(req,res) and Relative Refs
Date: Sat, 4 Sep 1999 18:09:51 -0700
>Leo Neumeyer wrote:
>
>> I read many postings from people having problems using
>> getServletContext().getRequestDispatcher(targetPage).forward(req, res);
>>
>> The problem seems to be that relative references in the target page will
>> not work because the base path is the original servlet path and not
>> the path of the target page. (Am I right?)
>>
>> Has anybody found a way to get forward() to work with static or JSP pages
>> that contain relative references?
>>
>> I'm using JRun with its builtin web server on Windows.
>>
>
>I can't speak for JRun in particular, having not used it, but you might want to
>read the API documentation for ServletContext.getRequestDispatcher() , quoted
>below in part:
>
> The pathname must be in the form "/dir/dir/file.ext".
>
I am using a relative path like "/dir/dir/file.ext". This works fine as
long as "file.ext" does not contain a relative reference. However if
"file.ext" contains, for example:
<img src="picture.gif">
then the web server tries to find it in:
http://localhost:port/servlet/picture.gif
instead of the doc root:
http://localhost:port/picture.gif
>
>If you specify a correct pathname, it is interpreted as relative to the context
>path for the servlet context in which you are operating. See the "Clarifications
>to the Servlet API 2.1 Specification" in the JSP 1.0 spec for more information on
>this.
>
I read this clarification and found the solution. This is roughly what the
spec says:
"...If the relative URL starts with with no '/', then the path is
calculated relative to the servlet dir, if it starts with '/' then the
URL is calculated relative to the DOCROOT..."
All I had to do to fix the problem is to add the '/' to the relative
ref, for example:
<img src="/picture.gif">
Thanks for the pointer Craig!
>Craig McClanahan
>
Leo Neumeyer
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html