tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Logo file location
Date Mon, 14 Dec 2009 22:02:22 GMT
Hash: SHA1


On 12/14/2009 3:53 PM, André Warnier wrote:
> Upon further examination of the spec page above, I would guess that a
> better way (more flexible) would be to specify the relative URI of your
> file as an <init-param> value, and then do a getRealPath() to convert
> that into a real path, and then call getResourceAsStream() to get a
> stream opened on it.

By way of seconding Chuck's harangue, I feel like anytime you start
looking at using context.getRealPath for something, you're making a mistake.

>    InputStream logo_stream =
> context.getResourceAsStream(context.getRealPath(context.getInitParm('thelogo')));

Note that this would almost never work, because of these quotes from the
servlet API javadoc:

Regarding ServletContext.getRealPath:
This method returns null  if the servlet container cannot translate the
virtual path to a real path for any reason (such as when the content is
being made available from a .war archive).

(The above is the basis of Chuck's objection, with which I completely
agree: if your webapp can't be deployed as an un-exploded WAR file, then
you should fix it so that it can be.)

Regarding ServletContext.getResourceAsStream:
The path must be specified according to the rules given in getResource.

Regarding ServletContext.getResource:
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.

If your code above were to run, getRealPath would translate a "virtual"
path to a real path, and then the real path would be used as a virtual
path to fetch the desired resource.

The call to getRealPath is unnecessary and troublesome. Without that
call, the rest of the code ought to be perfectly fine:

InputStream logo_stream =

...with accompanying error checking, etc. Nobody likes NPEs ;)

I might also suggest the use of ServletContext.getResource instead of
ServletContext.getResourceAsStream, since that allows you to get more
information about the file other than its contents. For instance, if you
want to know how big the file is, you may be able to get that
information from the URL whereas you will won't be able to get it from
the file's contents without reading them to completion.

- -chris
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla -


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

View raw message