> From: André Warnier [mailto:a...@ice-sa.com]
> Subject: resource file location (was: Logo file location)
> 
> The trouble is, it does not really explain what is meant by a "path" in
> that context, and some of the other method descriptions on the same
> page introduce a slight doubt.

Sure it does:

"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.

"The servlet container must implement the URL handlers and URLConnection 
objects that are necessary to access the resource."

The path parameter is clearly a URL for any resource in the domain of the 
context.  What the URL actually maps to depends on the configuration of the 
webapp and container.

> If it is a real absolute file path on the hosting system

There is no requirement and certainly no guarantee that the host even has a 
file system.

> if on that system CATALINA_BASE is "/srv/tomcat/", then supposedly the
> parameter needs to be specified as
> <init-param>
>    <param-name>thelogo</param-name>
>   <param-value>/srv/tomcat/webapps/myapp/logos/mylogo.jpg</param-value>
> </init-param>

Nope.  Read the API spec again: "interpreted as relative to the current context 
root."  The proper setting is just <param-value>/logos/mylogo.jpg</param-value>.

> That seems to me in flagrant contradiction of the "encapsulation" ideal
> of a webapp and even of Java.

It would be, which is why the spec doesn't do it that way.

> and then used code like
> 
> InputStream logo_stream =
> ctx.getResourceAsStream(ctx.getRealPath(ctx.getInitParameter('thelogo')
> ));

Again, getRealPath() should NEVER (well, almost never) be used, since there's 
no requirement for it to return anything useful.

> So what is one to do, if one wants not to have to change the content of
> one's war file for each target host, but still allow the webapp to find
> and read the logo ?

You don't - you just have to read the spec correctly, and not ignore the 
definition of the parameter.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.

Reply via email to