I am having trouble to grasp the concept of relative and absolute paths associated with RequestDispatcher. Please help. The Servlet API says that ServletRequest.getRequestDispatcher() can take both relative path and absolute path. If the path starts with a "/", it is interpreted as relative to the current context root. The same Servlet API also says that ServletContext.getRequestDispatcher() must take a path that begins with a "/", which is also intepreted as relative to the current context root. Furthermore, the API says that the difference between the above two methods is that the former method can take a relative path. NOW HERE IS MY CONFUSION: since both methods can take a path that begins with a "/", apparently, a path that begin with a "/" is treated as an ABSOLUTE path. If this is a case, why such an absolute path "is interpreted as RELATIVE TO THE CURRENT CONEXT ROOT"??? Can anyone give me an example of how a relative path looks like? Thanks!
Originally posted by Sam Zheng: Furthermore, the API says that the difference between [ServletRequest.getRequestDispatcher() and ServletContext.getRequestDispatcher()] is that the former method can take a relative path. NOW HERE IS MY CONFUSION: since both methods can take a path that begins with a "/", apparently, a path that begin with a "/" is treated as an ABSOLUTE path. If this is a case, why such an absolute path "is interpreted as RELATIVE TO THE CURRENT CONEXT ROOT"???
Why the difference between the two methods? Because the ServletRequest is associated with a request, it has something a path can be relative to: the location where the servlet was invoked. The ServletContext, on the other hand, describes the web application; every servlet executes in the same ServletContext. A path has nowhere to be relative to, except the root of the web-app itself. A web-application is a self-contained unit, which can be deployed in any location. Ideally it should work no matter whether you mapped it to the server root ("/") or not ("/development/testing/webapp1/"). This will work only if "absolute" paths used in forwarding and inclusion take the web-app root as their starting point. The sticking point probably is that the term "absolute" is really only relative Every absolute path has to take a certain arbitrary starting point. In Unix, the starting point for an absolute filesystem path is a single root partition; in Windows, every partition has its own starting point. For a browser, an absolute path on your server starts at the server root; for a Java servlet/JSP, every web-app has its own starting point. You asked for an example. Take the web-app mapped to "/development/testing/webapp1/". Inside this web-app, a servlet is mapped to "/servlets/myservlet", which forwards the requests to "/servlets/myotherservlet".
Using the ServletRequest object, myservlet can forward the request to "myotherservlet". This will be interpreted relative to the location of "myservlet".
Using either ServletRequest or ServletContext, myservlet can also forward the request to "/servlets/myotherservlet". This absolute path takes the web-app root "/development/testing/webapp1/" as its starting point.
BTW, ServletContext.getContext() gives you access to other web applications running on the same server; that's how you dispatch requests to an arbitrary location on the server. - Peter
[This message has been edited by Peter den Haan (edited May 14, 2001).]