Hi. I'm back.
I've been experimenting with various options and have been asked
(via private mail) to summarise.
To recap on the problem:
There is bulk data in the form of static web pages
located in a remote directory to the WEB APPLICATION.
This calls for two separate contexts
(say appContext and dataContext).
In certain cases, a request is received by a servlet
which then need to return the main page of this data
to the client.
The data is organised using RELATIVE href links.
If the main page were accessed statically, these links
would be resolved relative to its location.
As the page is returned by a servlet in a different context,
there are problems with subsequent navigation.
It is not an option to rewrite the main data page
(there are actually many data areas to access,
and the control of their formatting lies with another project).
I am using Tomcat stand-alone, both on Unix and on NT.
Of the solutions below, sendRedirect() is the best:
//-------------------
// General setup (only high level stuff shown)
ServletContext appContext = getServletContext();
String host = "hhhh"; // set as required
String port = "pppp"; // "
String dataContext = "dddd"; // "
//-------------------
// 1. Redirect to the data page xxx.html
// located in the directory xxx relative to the
// data context defined as "dddd" in server.xml.
// This method works, and subsequent relative links also work.
// The client can also BACK and FORWARD across this request.
String xxxURL = "http://" + host + ":" + port + "/" + dataContext +
"/xxx/xxx.html";
response.sendRedirect(xxxURL);
//-------------------
// 2. Send to a short-lived html page with its own automatic
redirection.
// The objective is to change the BASE directory of the page
// sent to the browser so that subsequent relative URL links work.
// This works but the user suffers a redundant page and further
delay,
// and needs to double click on BACK to go back. Also easy to induce
errors.
RequestDispatcher xxxJSP =
appContext.getRequestDispatcher("/jsp/xxx.jsp");
xxxJSP.forward(request,response);
/* /jsp/xxx.jsp looks something like this (ignore the XMP tags):
<XMP>
...
<head>
<meta http-equiv="refresh" content="1;URL=/dddd/xxx/xxx.html">
</head>
<body>
This page will automatically forward after a brief delay
</body>
...
</XMP>
*/
//-------------------
// 3. Attempt to access bulk data through a symbolic link
// in the base directory of the application context.
// Works on Unix. Does not work at all through an NT shortcut.
// This works for the first page, but subsequent relative links
// are appended to the original (ie application) context.
RequestDispatcher xxxPage =
appContext.getRequestDispatcher("/misdata.lnk/xxx/xxx.html");
xxxPage.forward(request,response);
//-------------------
// 4. Attempt to forward the request using a request dispatcher
// in a foreign context.
// Works on Unix. On NT, attempt to get context for ("/" +
dataContext)
// just returns the current context again: Tomcat bug?.
// This works for the first page, but subsequent relative links
// are appended to the original (ie application) context.
ServletContext xxxContext = appContext.getContext("/" + dataContext);
RequestDispatcher xxxPage =
xxxContext.getRequestDispatcher("/xxx/xxx.html");
xxxPage.forward(request,response);
//-------------------
// 5. Use the BASE tag in the data main page to explicitly set
// its base value, rather than rely on picking up a default.
// (this solution not tried yet). In this case maybe solution
// number 4 above will work.
//------------------
Conclusion for relative url links, without an initial '/', on a static html
page:
eg <a href="yyy/zzz.html">zzz</a>
located on page http://myhost:8080/dddd/xxx/xxx.html
1. If the page is accessed from a browser, eg a GET request,
then relative urls on this page are resolved with respect to
the directory containing the page, ie .../xxx/yyy/zzz.html
2. If the page is accessed from a servlet, then:
2.1 If forwarding through a RequestDispatcher, the root will default to
the
context of the servlet, and relative urls will not work,
ie <path-of-appContext>/yyy/zzz.html
2.2 If using sendRedirect, the root will be the directory containing the
target page.
2.3 The root of relative urls can be forced by using a BASE tag in the
page,
ie <path-of-BASE>/yyy/zzz.html
So? Is this right? any comments anybody?
Thanks.
mailto:[EMAIL PROTECTED]
________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html