0

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!

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by EloiseJoseph
0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.