• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing directories using a JSP

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to get a directory list using a JSP. The directory (called 'xml')is directly beneath the webapps directory (in Tomcat 5). For some reason the code works when not in a JSP. Put it into a JSP and it doesn't. Here's the code:
<%
java.util.ArrayList lessons = new java.util.ArrayList();
try {
java.io.File file = new java.io.File("xml");
String [] files = file.listFiles();
for(int i=0; i<files.length; i++) {
out.println(files[i] + "<br>");
if(files[i].startsWith("jav") && files[i].endsWith(".xml")) {
lessons.add("xml/" + files[i]);
}
}
} catch(Exception ignore) {
out.println("Error: " + ignore);
}
request.setAttribute("lessons", lessons);
%>
The list() method doesn't seem to return anything (resulting in a NullPointerException when I try and access it). Any clues?
Ta!
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code makes the erroneous assumption that the 'current working directory' for the JSP is the webapps folder.
Check out the getRealPath() method of ServletContext for a way to map relative paths to real ones.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. UBB code tags are our friends! Please use them whenever posting code fragments.
 
Tony Walters
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that. Now works fine.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic