This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes relative and absolute path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "relative and absolute path" Watch "relative and absolute path" New topic
Author

relative and absolute path

siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Hi ,
I have a question
In request.getRequestDispatcher(path) in order to create it we need to give the relative path of the resource. But in resourcecontext.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.

what does the absolute path means ..Is it the path from context root to the resource?
And what does the relative path means ..which one the current directory..

Can you please explain by giving an example?

Thanks,
Siddharth
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518
    
    2

siddharth das wrote:Hi ,
I have a question
In request.getRequestDispatcher(path) in order to create it we need to give the relative path of the resource. But in resourcecontext.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.

what does the absolute path means ..Is it the path from context root to the resource?
And what does the relative path means ..which one the current directory..

Can you please explain by giving an example?

Thanks,
Siddharth


May be this will help. I'm sure you will find questions posted on this topic in javaranch.


Regards
KumarRaja

Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

  app
       |
       |----JSP
       |       |---Check.jsp
       |       |---Home.jsp
       |
       |----HTML
               |---Contact.html
               |---Downloads.html

Let this be the directory structure of you web application "app" with two subdirectories JSP and HTML containing Check.jsp & Home.jsp and Contact.html & Downloads.html.

The ABSOLUTE PATH of Check.jsp would be "/JSP/Check.jsp"and that of Contact.html is "/HTML/Contact.html". The absolute path always start with a slash "/". This is the path of your resource from the root directory of your web application.

Proceeding now, lets say you are accessing Check.jsp and from there you want to forward to Home.jsp using Request Dispatcher then the relative path would be simply "Home.jsp" or "./Home.jsp"(since, you are already accessing in the JSP directory). If, you want to forward to Downloads.html then the relative path would be "../HTML/Downloads.html".
Here "." means the current directory, and ".." the parent directory.

NOTE: The relative path never starts with a slash "/". It is always relative to the current directory your resource lies(i.e. the one from where you forward, include or redirect).
Hope you get it.


SCJP 1.6(91%), SCWCD 1.5(100%), SCBCD in progress
 
 
subject: relative and absolute path