On Fri, Oct 08, 2004 at 03:07:01PM +0200, Giuseppe Briotti wrote:
: I need to retrieve a file from inside a servlet. If I try:
: 
: Url myURL = servletcontext.getResource("/WEB-INF/myFile.xml");
: File myFile = myURL.getFile();
: 
: If I try this, I obtain a different behaviour depending on 
: the web container:
: [snip]
: Thus, I think that there is a better method to retrieve the file,
: isn't it? 
: 

There are three reliable ways to do this:

1/ put the specified file under the classpath, such as in a JAR or under
WEB-INF/classes, such that you can fetch it using getResourceAsStream().
This is suitable if you don't want the data to be accessible to clients
via GET requests.

2/ Use the RequestDispatcher to forward() to the data (if you want
clients to get just that file) or include() it (if you're putting
several different files' data in the response).  This works well if the
file in question is under the document root, that is, it would otherwise
be accessible via a client's GET request.

3/ Open an InputStream to the file's absolute path, and push that to the
client via the Response object's OutputStream.  This works best if the
specified files exist outside of the webapp context, since the webapp
may be sealed inside a WAR and thus the notion of "absolute path" goes
out the window...

-QM
-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to