On 14/12/2009 22:02, Christopher Schultz wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
André,
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 =
context.getResourceAsStream(context.getInitParm('thelogo')));
...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.
The last draft of Servlet Spec 3.0 I read had some stuff in it about
finding/putting static files in META-INF/resources. Might be worth
considering as a future-proof(ish) location for such things.
p
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAksmte4ACgkQ9CaO5/Lv0PBSUACgg6ncXhP8q1oTTbdorJzeEho7
2t0An1hwCrDNW2kkek0zR37oXJ5P/WgG
=6z0y
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org