How to list contents of a server directory using JSP?

37,627

Solution 1

you should know the path of the jsp within your web application so you can pass that to getRealPath()

File jsp = request.getRealPath(pathToJspInWebapp);  //eg. /WEB-INF/jsp/my.jsp
File directory = jsp.getParentFile();
File[] list = directory.listFiles();

Solution 2

        <%@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>");
        %> 

Replace "C:/mydirectory/" with your directory

Solution 3

As of Version 2.1 of the Java Servlet API use:

File jsp = new File(request.getSession().getServletContext().getRealPath(request.getServletPath()));
File dir = jsp.getParentFile();
File[] list = dir.listFiles();
Share:
37,627
ivan_ivanovich_ivanoff
Author by

ivan_ivanovich_ivanoff

!!! Netbeans 6.7 RELEASED !!! C is far better and easier than Java! Why? It is easier to use void pointers and to do pointer arithmetic, than explaining the basic concepts of OOP to a C programmer. Joke of the century: PHP is good, because it works... "PHP programming" is an oxymoron. There is no PHP programming. There is only PHP scriptkidding. There are two types of people who use PHP: - those who don't know other languages, - and those who HAVE TO use it Java is to JavaScript what Car is to Carpet. The LHC is the wrong answer to the technological singularity.

Updated on July 24, 2020

Comments

  • ivan_ivanovich_ivanoff
    ivan_ivanovich_ivanoff almost 4 years

    When writing a JSP file, how can I get the current directory of this file at runtime
    (to be able to iterate the directory and list its contents)?

    Would some file I/O operations be restricted because of some security issues?

    I would prefer a solution without accessing some implementation-specific server variables / properties.

    EDIT:
    I wouldn't ask if it were as simple as new File("."), because this would just give the directory of server's executables.

  • Michael-O
    Michael-O almost 11 years
    Attention: This will fail when the WAR file is not exploded!
  • chrips
    chrips over 5 years
    NOTE: .getRealPath() is deprecated from Version 2.1 of Servlet API coderanch.com/t/365382/java/…