File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Retrieving a directory listing from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retrieving a directory listing from JSP" Watch "Retrieving a directory listing from JSP" New topic

Retrieving a directory listing from JSP

Michael Arnett
Ranch Hand

Joined: Mar 22, 2001
Posts: 65
Hi all,
simply put is this possible from a specific object (ie Directory or File Object) from within JSP? I realize there are ways of doing it with Runtime object and "ls" / "dir", but I was wondering if there was any "cleaner" way than parsing the returning stream from these processes. Thanks in advance!

Sun Certified Programmer for the Java 2 Platform 1.4
Sam Dalton
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
I would have thought you could do this from JSP exactly the same way as you can from a Java App. Create a new object (new File (path)), call the listFiles method and use the returned array of files to print out the names (through the use of File.getPath() or such like.
Not sure on the directories that you will be able to see. Give it a go and see.

<a href="" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of []Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
Trevor Dunn
Ranch Hand

Joined: Jun 13, 2001
Posts: 84
You can definately do this is jsp, the way Sam has described. I have done it.
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
Sure, you can do this, but the directories that you will see will be from the server that the JSP is running. You wont be able to see the client's directory structure since the JSP is compiled and executed on the server, so dont waste your time if this is your intention.
I agree. Here's the link:
subject: Retrieving a directory listing from JSP
It's not a secret anymore!