• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RequestDispatcher, relative and absolute path

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".

  • A user can access the first servlet through the URI "http://yourhost:80/development/testing/webapp1/servlets/myservlet".
  • 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).]
 
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic