"Chen, PeterX" wrote:

> Can someone explain to me exactly what the path String parameter is?  The
> 2.1 servlet API states that path is a virtual path.  Can someone give me an
> example of one?  Thanks.
>

A "virtual path" in this context means the part of the URL after the hostname
(and optional port number).  For example, in the URL:

    http://www.mycompany.com:8080/images/banner.gif

the "/images/banner.gif" part would be a virtual path.  It's virtual, because
the web server you are running in can do all sorts of interesting redirections
to identify where the file really is -- it might not actually even be a file.

The point of the getRealPath() call, then, is to translate this "virtual" path
into an absolute pathname that you can use in as a pathname argument to
something like FileInputStream(pathname).  This is not always possible, so the
call might return null.

The fact that getRealPath() returns a pathname is quite nice, but can be
limiting.  The recommended method to read contents of resources is to use
getResource() or getResourceAsStream() instead.  The advantage is that
getResource() returns a URL to the resource, which might be a file reference
("file://usr/local/images/banner.gif") or it might be a reference to a
resource on some other web server.  Consider, for example, a servlet 2.2 based
application that is running directly out of a web archive file (JAR file with
special contents).  You can still use getResource() to access the contents of
an image packaged in the archive, even though it has no pathname that can be
returned by getRealPath().

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to