• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

question on getResource

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic