• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2EE - Relative Path Vs Absolute Path

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Path starting with / -> Absolute path where / represents the document root of your application.

2. Path starting with ../ -> Relative path, i.e. relative to the current request.

Ques 1. W.r.t 1., will '/' always refer to the document root of our web application? Will our-web-application always be the document root?

Ques 2. w.r.t 1. Consider our-web-application is the document root. If we want to access a html file, then by /login.html, we expect login.html to be present under <our-web-application> (i.e / represents <doc root> BUT when we say /servlet/LoginServlet then we expect LoginServlet to be present under /Web-INF/classes/servlet. Why in this case the <doc root> is changed (surely it is not our-web-application as / represents classes)? Moreover what is / represents in this case ?

Ques 3:
By the statement
req.getRequestDispatcher("../login.html");

what is the current directory so that we can know login.html should be one level up?
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Path starting with / -> Absolute path where / represents the document root of your application.

Firstly, Path starting with '/' is not absolute path, its relative path. Absolute path include the server name/ip and port etc like http://www.me.com:8080/MyApp.
Secondly, yes '/' represents the document root of you web application.

2. Path starting with ../ -> Relative path, i.e. relative to the current request.

Yes, this is also relative path, '..' denotes one directory up to the current directory

Ques 1. W.r.t 1., will '/' always refer to the document root of our web application? Will our-web-application always be the document root?

Yes '/' will always refer to document root i guess, please correct me if i am wrong

when we say /servlet/LoginServlet then we expect LoginServlet to be present under /Web-INF/classes/servlet.

No, it is not required to place it under /WEB-INF/classes/servlet, you can place it under any package in classes folder.

Why in this case the <doc root> is changed (surely it is not our-web-application as / represents classes)? Moreover what is / represents in this case ?

Servlet's case is somewhat different, you need to specify a URL Mapping for a servlet to access it, and also provide the fully qualified class name to access it, it doesn't require you to put servlet in any specific package. Document root is still the same '/'

By the statement
req.getRequestDispatcher("../login.html");
what is the current directory so that we can know login.html should be one level up?

if you request came as http://someserver:8080/YouApp/admin/pages/index.jsp then it will find login.html in admin directory i.e. one directory up to the current directory of requested page.
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice Explanation Ali
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic