Originally posted by Stephane Philippart:
Ben : I agree with you for the specs .... your code seems to work perfectly why are you saying :
I didn't feel comfortable putting it into production myself so..buyer beware:
?
Stef
I just don't feel comfortable parsing the results of that call to find the contextPath.
If you're going to use that, I would recommend putting some checks to make sure that you get back a
string that can be parsed that way.
Also, I'm not sure what comes back if the app is deployed from an un-exploded war file.
I just tried putting that in a
jsp scriptlet and ran it from the default application (ROOT) and it returned: "localhost".
Probably not what you would have expected.
My point was that getResource is meant to return a URL to a resorce within the webapp. It was not intended that developers would parse a string representation of the URL to pluck the contextPath from it.
Any time you rely on an uninteneded side effect of a method, you risk having it come back to bite you.
If you're relying on the value for something important, make sure you do a lot of
testing, in any container that you intend to support.