Howard Melman wrote:
> I have a question which I think is a flaw in the servlet spec.
>
> ServletContext has a method:
> URL getResource(String path) throws MalformedURLException
>
> It seems that this should construct an URL using the Context
> URL as the base and the path as the extra info. Some
> massaging needs to happen to make sure the path is treated
> as context relative and not server relative, but okay. And
> since this method throws MalformedURLException this seems to
> confirm my thought.
>
It doesn't have to be based on the context URL at all. The way most servlet
engines implement this is that you configure the "base" URL for resources in
the setup of your servlet context, and the getResource() method then tacks on
your context-relative path on the end.
>From the servelet developer's perspective, you don't care where the context
gets its resources from ... all you care about is the relative path from
within that base.
>
> But, the URL must have a scheme and what should that be?
> Assuming that this object is on the server then it might be
> available via both http and https, for example. Which form
> should the method return? It can't guess from the request
> since this object is associated with the application and not
> a single request. Just assuming http doesn't seem the right
> thing either.
>
An "http:" scheme is certainly legal, if you want your servlet context to get
its resources from a different web server. A "file:" scheme works great if
the resources are in disk files on your local server -- which essentially
eliminate the need for getRealPath(). The Java language also lets the servlet
engine provider create custom handlers for schemes you make up -- for
instance, you could define a handler for a "database:" scheme that grabbed the
resources out of a database directly. The whole concept is quite flexible.
>
> I'm confused. What am I missing?
>
If you are writing servlets, you don't care -- that's up to the engine. All
you need to do is tell the servlet engine where to find your resources when
you configure it.
If you are writing a servlet engine, I would suggest you do configuration
something like the way the JSWDK engine does. For example, in the
"default.cfg" file, you declare the "document base" for the default context
using the server.docbase parameter. This value also serves as the base URL
for getResource() call resolution.
Most people just use a directory name here, which gets turned by the server
into a URL that starts with "file:". But, you could also put any other legal
URL here (such as "http://www.mycompany.com/images"), and your servlets would
auto-magically get their resources from the named location on the named web
server, with no changes at all in the servlet code.
>
> Howard
>
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