Hi everyone!
I am making a JSP page with Eclipse and Apache Tomcat 6. I want to create a drop-down list that will be populated with the list of all folders in the directory of that JSP page. Can someone show me how to do that?

Thanks in advance!

You could try this code:

<%-- 
    Document   : index
    Created on : Jul 27, 2010, 2:09:50 PM
    Author     : jaka
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.File" %>
<%@page import="java.io.FileFilter" %>
<%@page import="java.io.IOException" %>
<%
String currDirStr = getServletContext().getRealPath("");
File[] folders = null;
try {
    File currDir = new File(currDirStr);
    if(currDir.exists()) {
        folders = currDir.listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                String name = pathname.getName();
                if(pathname.isDirectory()) {
                    if("WEB-INF".equals(name) || "META-INF".equals(name)) {
                        return false;
                    }
                    return true;
                }
                return false;
            }
        });
        if(folders == null) {
            folders = new File[0];
        }
    }
} catch(Exception ex) {}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">



<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="GET">
            <table border="1">
                <tbody>
                    <tr>
                        <td>Select Folder:</td>
                        <td>
                            <select name="folder">
                                <%
                                for(File folder : folders) { %>
                                <option value="<%= folder.getAbsolutePath() %>"><%= folder.getName() %></option>
                                <% } %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <input type="submit" value="Submit" />
                        </td>
                    </tr>
                </tbody>
            </table>

        </form>
    </body>
</html>

Thanks I also have same Problem. I try it code and share then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.