Error displaying image

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();

        else {
            System.out.println("directory is empty");
        req.setAttribute("imgUrlList", imgUrlList);

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test/test.jsp");
        dispatcher.forward(req, resp);


I’m not sure, but i have a feeling that slash in front of your image directory is the issue. If images is a root level directory it’s fine, but if it’s a subdirectory of your project you might want to try just “images” or maybe “./images”. You may also need a trailing slash (“images/”)