aspose file tools*
The moose likes Servlets and the fly likes question on getResource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "question on getResource" Watch "question on getResource" New topic
Author

question on getResource

Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Hi everynoe,

This is from API doc about ServletContext.getResource(String)

Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.


What I don't understand here is that it is relative to the current context
root and at the same time the resource can be local, remote, in database
or in a .war. I thought that relative to context path means that it starts
with "/" and that means that I have access starting from the root of
my application which would exclude remote and other resources ....

Could someone please explain it ?

Cheers,

Adrian
[ April 08, 2008: Message edited by: Bear Bibeault ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

We are referring to a resource packaged in the web application, and accessing resource in that packaged web application. It is not referring to the HTML location for the resource.

For example a web application may exist as directories and files in the container's wepapps directory, and the file WEB-INF/conf/my.properties would return a URL in the form file:///path/to/container/webapp/mycontext/WEB-INF/conf/my.properties - Note that resources in the WEB-INF directory are not available via HTTP.

A web application that is packaged as a WAR file and not unpackaged by the container would return a URL in the form jar:///path/to/container/webapp/mycontext.war!/WEB-INF/conf/my.properties - note the reference to the file within the WAR file
(this is most likely wrong, but it looks something like that)

You can't fet a java.io.File reference to a file within a WAR or JAR, but you CAN get a URL pointing to it.

If the web application loads the resource from a remote resource, then the URL will refer to this remote resource. I'm not sure how you get a URL to a database resource, but I am keen to be enlightened.
Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Hi David,

Thanks for your reply !

Yes - I agree with what you say, but what I mean is, according to the
documentation and spec, the URL has to start with "/". How can an URL
refer to a remote resource when it starts with "/" ? That's what I don't
understand ....

Cheers,

Adrian
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The path must begin with a "/" and is interpreted as relative to the current context root.


Meaning local path to the resource you want a reference to must contain a "/" at the start. Nothing to do with a slash on the URL.

If the resource was loaded from a JAR that was loaded from a remote location, the URL could be ftp://daves.machine.com/suspiciousCode.jar!/my.file
Adrian Sosialuk
Ranch Hand

Joined: Jun 07, 2007
Posts: 57
Yes - my bad ...

I meant of course path - not URL.

Still:

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.


is not quite clear to me. How can you get a resource from a remote
file system for example when its path has to start with "/" ?

Cheers,

Adrian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on getResource