This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on getResource

 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic