A web server is not a file server. So being able to find the name of a directory on the web server is pretty meaningless for a web client - there's no way for the web client to access that directory or its files directly unless file-serving software is also installed on the web server machine, the client has an appropriate file-server client software (filesystem driver) and the necessary network and user security privileges.
So no, there's little utility in being able to get the server directory pathname in a JSF or JSP page. Also, when the "directory" is actually part of a WAR, it may not exist as a physical disk directory item at all.
In general Java, there are several methods of getting a directory's path. The most common way is with the java.io.File.getAbsolutePath method. For example:
String pathString = new File(".").getAbsolutePath();
will set pathString to the filesystem path of the current working directory (".").
An IDE is no substitute for an Intelligent Developer.
Hmmm. I think I misread that - it sounds like you want to get a directory name from the client's file system and send it to the server.
The primary issue is figuring out what "the" name should be. Probably the working directory of the web browser, but that's generally not likely to be useful to the server.
Without a signed applet or similar construct, the browser can't reach outside itself to get filesystem info. The closest it can get is when you use the HTML file upload facility, which means actually sending a file, and I don't recall if the meta-data for the upload includes the client's directory path or not. If it does, you can take it and do a "new File(uploadname).getParent.getAbsolutePath();"
Both of Tim's guesses seem pretty useless to me. The client can't do anything useful with the name of a directory on the server, and the server can't do anything useful with the name of a directory on the client.
Could you clarify your question? Was it either of those two? If so, why? Or if not, what did it mean? (Your word "local" isn't very precise... "local" to what?)
Joined: Jan 13, 2008
Thanks Tim and Paul for your responses. Let me be more clear in explaining my objective.
For example if I have the following directory structure in my PC as C:\dirNames\test1, test2, .. testN
In my browser, I have to allow the user to see the list of files under as a drop down list which includes test1, test2... testN. And when the user selects any one item in the list, I want to write that value into an sql database. Along with this values, I will be collecting some more details from the user in the same page.
But my main goal would be in displaying the list from a particular path. Is it possible to do with JSF? or in JSP? Please let me know.
No, it's not possible in JSF or JSP, because they do their work on the web server and the web server cannot access your local file system.
As I mentioned in my second post, your options in accessing local system resources from a web browser are limited, for security reasons - the so-called "sandbox".
In order for a Java solution to work, you'd need a Java applet, which is code running inside the browser system on the client computer. However, the applet sandbox rules expressly prohibit access to the client computer's filesystem, so you'd have to create a signed applet. Then you have to have someone manually permit the applet to run, which last time I looked was a cumbersome process that casual users wouldn't be able to manage.
In fact, no browser-contained application system should be able to read or write files or otherwise gain access to the local directory tree without strict authorization for the very simple reason that that is exactly the mechanism that facilitates the installation of malware and causes data leaks. Unfortunately, the bad guys have made life difficult for legitimate applications.
There is, by the way, another reason why this sort of interaction isn't better supported. In an enterprise environment, user computers are just about the last place you want to store critical business assets. They frequently don't get backed up, the data cannot be easily shared (depnds on how you define "easy", though), data access control is potentially poor, and, finally, in the case of laptops, you have the risks of loss, theft or damage of the computer and the data in it.
I think, however, that the real question is "what specific benefit do you get from knowing what's in the user's filesystem"? Since you're not actually transferring the files, would it be possible to simply create a "virtual file tree" stored in the database or somewhere that the server can access?
Joined: Jan 13, 2008
As a beginner I tried something like below. I just need the names of all the directories under C:\testing and have to display thelist for the user and when the user selcts one directory, I have to pick that value and store in the database. Righnow I am juts trying to display the value in another page and database would be my next step.
In my JavaBeans.java class apart from many beans, I have added this method below to get a list of directory names as alist test. I have to include exception part in future.
While trying to display this list of directories in my jsf page I was successful in the initial setup as below. Displaying the list inside a input box.
I could see all the directory names separated by comma.