File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Difference between real path and virtual path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Difference between real path and virtual path" Watch "Difference between real path and virtual path" New topic
Author

Difference between real path and virtual path

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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

Joined: Aug 15, 2004
Posts: 2874
Its not virtual, it is relative.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 10, 2002
Posts: 61774
    
  67

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between real path and virtual path