views:

244

answers:

2

Can JNDI be used in a Java servlet to access filesystem on the local machine or a remote machine?

I am able to bind local directories/files with it, but not able to find a mechanism(if exists) to read/modify the file's contents.

The files are simple text files.

Please tell me if it is possible, and how?

A: 

JNDI is an API (i.e. an interface) - implementations may vary in what they allow you to do. I think that generally JNDI implemnattions are for resource allocation and discovery and not used for this purpose.

oxbow_lakes
+1  A: 

As its name implies, JNDI provides Java applications access to directory and naming services. It is targeted for retrieving resource names from the directory. Java applications typically use JNDI to look up JDBC data sources, mail sessions and for authenticating and authorizing users. I suppose you could store a custom object in the directory, but this is not the recommended way of things.

For local files you could add a String resource with the name and path of the file, retrieve it with JNDI and read/write it with standard means.

kgiannakakis
Can 'adding a String resource with the name and path of the file' done for a remote filesystem?
Mohit Nanda
If this is a public url yes. You could use that and download the file to read it. You can't edit it however, unless the remote system allows something like FTP or HTTP PUT.
kgiannakakis