views:

87

answers:

1

How do I populate a dropdown list (in a JSP page) dynamically with all the folders present in the same directory as the JSP page, on Apache Tomcat?

A: 

use servletContext.getRealPath("/someuidir-in-your-webapp/somejspdir") to get the absolute path of the directory of that jsp and then use the java.io.File and java.io.FileFilter (filtering directories) . Here's the javadoc for getRealPath

Adding some code (You can put this in your jsp). Note that this is just an example to get you started on how to do it. It may need some imporovement (in terms of design)

  <%
  File jspDir = new File(application.getRealPath("/WebContent"));
  File[] list =  jspDir.listFiles(new FileFilter() {
        public boolean accept(File path) {
           return path.isDirectory();
        }
  });

  for(File f : list)   {
     out.write("<p>" + "</p>");  // replace this with whatever way you
                                 // want to populate
  }
  %>
naikus
I managed to get the real path using <%= request.getRealPath("/WebContent") %>Now how do I use the directory filtering? Sorry but I'm a novice in JSP!
GPX
Thanks for the answer! I figured it out! Yay!!
GPX