• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between real path and virtual path

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i am using

String contextRootPath = this.getServletContext().getRealPath("/");

statement in my program.

where, getRealPath("/") Returns a String containing the real path for a given virtual path.

I would like to know the difference between real path and virtual path.

Can anyone help me in this regard ??
Please.........

Regards
Ashvini
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not virtual, it is relative.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Adeel says, it's relative path.
Relative to the root of your context (application).

ServletContext.getRealPath(relativePath) attempts to find the location of the given resource on the server's file system.

I say 'attempts' because J2EE apps are not always run from directory structures on a file system. They can be deployed as packed war files. When getRealPath is used from within such an app, it will return null.
 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
...
I say 'attempts' because J2EE apps are not always run from directory structures on a file system. They can be deployed as packed war files. When getRealPath is used from within such an app, it will return null.

Ben,

So why is it so when the war file explodes to its folder structure and resources. In that case each resource has a physical path(canonical). So can you clarify why you can't get "a real path".
If so then what you do acheive your task of finding "a real path".

-Swapan
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the war file is exploded, then getRealPath will work.
Not all app servers will explode the war file.
Tomcat, for instance, gives you the option.


From:
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/host.html

unpackWARs
Set to true if you want web applications that are placed in the appBase directory as web application archive (WAR) files to be unpacked into a corresponding disk directory structure, false to run such web applications directly from a WAR file. See Automatic Application Deployment for more information.


A simple rule of thumb is not to rely on getRealPath if you want your app to be portable.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ashvini .S",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic