Nic Ferrier wrote:
> Can anyone confirm the API behaviour of the:
> URL ServletContext.getResource(String path)
> call for me?
>
> Is it suppoosed to only return resources that are held in the servlet
> engine or any resource it can find?
>
The answer, I think, is "it is up to the implementation to decide". My
comments below are based on how I'm thinking about implementing this thing in
the next version of Apache JServ (which will support the 2.1 API) -- I would
also appreciate any comments on the conformance of this interpretation to the
spec.
>
> Eg: I have a a Servlet engine and I load a servlet called WWW mapped
> to .HTML (ie: a web server servlet).
>
> Then:
> URL p=ServletContext.getResource("/docs/fred.HTML");
>
> will return a URLHandler for the file fred.HTML using the servlet
> WWW.
>
Why are you trying to apply extension mapping to a getResource() call?
Usually, extension mapping is configured to select the servlet to execute
originally, based on the request URI. I suppose you could do it for resources
if you wanted, but what would it mean? For example, are you implying that it
should run the specified input file through your WWW servlet before giving you
the response? That sounds like what RequestDispatcher.include() is for.
IMHO resources are just data, which you are retrieving from a named path
somehow -- versus servlets, which are executable and where the concept of
choosing which servlet to execute based on the extension makes sense.
Also, I don't really want to nitpick, but it will return a URL rather than a
URLHandler. The URLHandler that is actually used depends on the "scheme" part
of the URL, and what handlers you have installed in your JVM. This is
transparent to the servlet -- all the servlet needs to do is openConnection()
on the returned URL to retrieve the data.
For Apache JServ, I am not going to assume that the resource request MUST
resolve to a file path. The plan is to provide a configuration parameter
(call it the "resource base") for a servlet context that tells the context
where to get the requested resources from. For example, if the resource base
is set to
file:///usr/local/apache/share/htdocs
then your getResource() example would return a URL for
file:///usr/local/apache/share/htdocs/docs/fred.HTML
which would end up (when you call URL.openConnection() on it) reading the file
directly. Presumably the specified directory is the document root for your
web server, but it doesn't have to be. You also cannot assume that the file
path really retrieves the same resource you would get by asking the web server
to get /docs/fred.HTML because the web server.
On the other hand, if the resource base were set to:
http://www.mycompany.com/resources
then the example would resolve to a URL object for:
http://www.mycompany.com/resources/docs/fred.HTML
instead. When you use the openConnection() call on such a URL, you will
retrieve the referenced resource through an HTTP connection to the specified
web server (which could be anywhere on your network), instead of reading it
from the filesystem.
Using this approach, you can use any sort of URL you want, as long as there is
a URLHandler that knows how to interpret the "scheme" part at the beginning of
the string. For instance, you could retrieve resources from an FTP server by
using an ftp:xxxxx resource root.
>
> But if I do this:
> URL p=ServletContext.getResource("/doc/fred.pl");
>
> I will get an error because fred.pl is not a resource that is
> available through the engine.
>
Assuming that you knew there was no way to get the appropriate resource, the
spec says you should return null instead of a URL. It should only throw a
MalformedURLException if there is a syntax error in the URL string itself.
What should you do, though, if your resource root (in the suggested
implementation for Apache JServ above) is an HTTP reference to a remote
server, so you don't know whether or not the resource is valid? My guess is
it is better to go ahead and create (and return) the URL -- the servlet will
encounter the error when it tries to retrieve the resource via
openConnection(). Otherwise, the getResource() code itself would have to call
the web server and do a conditional GET or something, which is redundant (and
bad for performance) in the normal case that the resource really is there.
>
> Is the above the behaviour or should the call return a URLHandler to
> fred.pl if it exists, despite the fact that the ServletContext doesn't
> have a Servlet to handle it?
>
As stated above, I don't see why you are trying to implement extension mapping
at the getResource() level -- to me, it only makes sense at the servlet
level. This includes the mapping that a RequestDispatcher has to do before
you call forward() or include(), so it sounds like it should accomplish what
you are after using extension mappings to just servlets.
Or is there some use for mapping extensions in getResource() that I haven't
thought about yet?
>
> Glad if anyone can help.
>
> Nic Ferrier
> Tapsell-Ferrier Ltd
> www.tapsellferrier.co.uk
>
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