Thomas Paul wrote:
> Just wondering, is there a reason you are accessing
> the file using the URL and not just reading it as you
> would read any file on a server? It seems odd to open
> a connection to the same server that the servlet is
> sitting on in order to read a file.
> --
>
There are at least five reasons one might do it this way:
* Reading the document as a file depends on you knowing
what aliasing your server is applying to URL names --
otherwise, the path name you compute won't be correct.
Using a URL is always accurate, because it goes back through
the web server again. (In many JVMs, there are optimizations
internally on socket connections to the same machine that
make this faster than it might otherwise appear).
* The assumption that the web server and servlet engine are
running on the same server is not necessarily accurate. For
example, Apache JServ lets you run Apache on one machine,
and JServ on another (or more than one "other" if you want
load balancing).
* Reading documents as a file might work on static content, but
you won't be able to read any dynamic output (from a servlet,
a CGI script, or using server-side includes for example).
* Reading documents as a file bypasses any web server access
controls on the file. Reading through a URL requires the
servlet to authenticate itself (with the "Authorization" header)
like any browser-based user would need to.
* Reading documents through a URL connection gets logged in
the web server's access logs (like any other hit). Reading documents
as files does not.
In the servlet 2.1 API, two new calls were introduced to make this even easier:
ServletContext.getResource() and ServletContext.getResourceAsStream(). They let
you specify a path relative to the base URL you specify (which is often the
document root of your web server), and give you back either a URL (so you can do
it yourself) or an InputStream (so you can read directly). In the case at hand,
you would still need to use the URL so that you can set the "Authorization"
header.
Why is this better? Because the path is *relative* to a configured base URL
that you set when you install your servlet-based application, instead of
absolute. This decision can be changed (for example, the webmaster might move
your resource files to a different "subdirectory" on the server) without
impacting your servlet code at all -- the only change required is a
configuration parameter to the servlet engine.
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