I am trying to display a list of images using servlet and JSP but I am getting the error exception handling message that the directory is empty. I have few images in the directory, I don’t know why I am receiving the error.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List imgUrlList = new ArrayList();
File imageDir = new File("/images");
File[] files=imageDir.listFiles();
if(files!=null) {
for (File imageFile : files) {
String imageFileName = imageFile.getName();
imgUrlList.add(imageFileName);
}
}
else {
System.out.println("directory is empty");
}
req.setAttribute("imgUrlList", imgUrlList);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test/test.jsp");
dispatcher.forward(req, resp);
}