Click to See Complete Forum and Search --> : show directory contents with JSP


beemee_freeserv
01-19-2005, 01:34 PM
what I want to do is (in JSP) to show the content of a specified directory
can anyone tell me how that is done?
I have absolutely NO JSP experience and I tried all day to find something that says how it should go, but i didn't get further than:

String jspPath = application.getRealPath(request.getServletPath());
java.io.File jspFile = new java.io.File(jspPath);
out.println(jspPath);

which shows the file i am using...so my guess is that i have to work with java.io.File, but how... :confused:
i have absolutely no idea (i also tried to find more with Google, but no luck so far...)

can anyone help me out?

ray326
01-19-2005, 11:38 PM
Look at the API doc for java.io.File, the list*() methods.

beemee_freeserv
01-20-2005, 03:49 AM
i already did, but i don't understand it...
but i did find what i needed somewhere else:


<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%!
public void GetDirectory( String a_Path , Vector a_files , Vector a_folders )
{
File l_Directory = new File( a_Path ) ;
File [] l_files = l_Directory.listFiles() ;

for( int c = 0 ; c < l_files.length ; c ++ )
{
if( l_files[c].isDirectory() )
a_folders.add( l_files[c].getName() ) ;
else
a_files.add( l_files[c].getName() ) ;
}


}
%>

<%
Vector l_Files = new Vector() , l_Folders = new Vector() ;
GetDirectory( "C:/mydirectory/", l_Files, l_Folders) ;

//folders should be left out...
//for( int a = 0 ; a<l_Folders.size() ; a++ )
//out.println( "[<b>"+l_Folders.elementAt(a).toString() + "</b>]<br>") ;

//generate files as XML
out.println("<music>") ;

for( int a = 0 ; a<l_Files.size() ; a++ )
out.println("<file>" + l_Files.elementAt(a).toString() + "</file>") ;

out.println("</music>") ;
%>