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
2010-07-22 05:28:31
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
2010-07-22 05:50:18
Thanks for the answer! I figured it out! Yay!!
GPX
2010-07-22 10:13:30