tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Warnier>
Subject resource file location (was: Logo file location)
Date Mon, 14 Dec 2009 22:37:50 GMT

On another thread, a discussion was started as to how to correctly 
locate and specify some data file, part of the application, and which 
needs to be read in by some code part of that same webapp.

Say that the application has a context path "/myapp".
The original idea is to locate the resource file for instance in a 
subdirectory "logos" of the web application, and for some flexibility to 
indicate this location to the webapp via an <init-param> in the webapp's 
web.xml, like


"ctx" being the ServletContext, the webapp could then obtain an input 
stream to this resource by a line like

InputStream logo_stream = 

Now the question is : how does this param-value need to be set in order 
for the getResourceAsStream() to work correctly ?
According to the specification which I find here :
we have 	getResourceAsStream(java.lang.String path)
       Returns the resource located at the named path as an InputStream 

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.

If it is a real absolute file path on the hosting system, and if on that 
system CATALINA_BASE is "/srv/tomcat/", then supposedly the parameter 
needs to be specified as


However, that is not very flexible.  If this file is an integral part of 
my webapp, then chances are that it is, relative to my webapp context, 
always at the same place. But in absolute host path terms, it is not, if 
my webapp is to be deployed on different hosts.
I would then have to change, inside of my webapp's web.xml file, the 
value of the <param-value> in function of the target host, before I 
create a war-file containing my webapp (and its web.xml), to deploy it 
on that target host.
That seems to me in flagrant contradiction of the "encapsulation" ideal 
of a webapp and even of Java.

So let's see if we can improve on that.
Maybe if we specify the parameter as follows


and then used code like

InputStream logo_stream = 

then according to

  java.lang.String 	getRealPath(java.lang.String path)
           Returns a String containing the real path for a given virtual 

presumably we should get what we want (at least if "virtual path" 
mentioned above has the meaning of "relative path under this webapp's 
context path" but that is not very clear either).

However, it has many times been indicated on this list that 
getRealPath() is not guaranteed to return anything.(*)

I can see still another way to possibly obtain an input stream on this 
file, which would be to use the

java.lang.String 	getContextPath()
           Returns the context path of the web application.

and then maybe add "/logos/mylogo.jpg" to it.  But I am not quite sure 
at this stage either what "path" means here (presumably just "/myapp"), 
like when mentioned with the "path" attribute of a Context element.

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 ?

(*) I understand that the path returned by this function could not be 
used as a path to create a file, or write to one, because it is not 
guaranteed to point to a writeable disk location.  But considering that 
this resource file is a part of my own application, which I supply 
inside of my war file which is expanded by the servlet container, and 
from which I only want to read, why can I not use this getRealPath() 
successfully then ?

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message