title: Display images on a JSP page through a servlet
body: <p>I am trying to display images in a Jsp page using Jstl, and image paths are passed on by a servlet. The Jsp page actually displays only one of the retrieved images and throws a NullPointerException.</p>
  
  <p>The Jsp looks like this:</p>
  
  <pre><code>&lt;c:forEach items="${images}" var="img"&gt;
      &lt;div class="col-md-3"&gt;
          &lt;img src="displayImg?imageId=${img.imageId}"&gt;
      &lt;/div&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>And servlet doGet method looks like this:</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      int imageId = Integer.parseInt(request.getParameter("imageId"));
      Image img = imageDao.getImageById(imageId);
      response.setContentType("image/" + img.getImageType());
  
      File f = new File(img.getImagePath());
      BufferedImage bi = ImageIO.read(f);
      OutputStream out = response.getOutputStream();
      ImageIO.write(bi, img.getImageType(), out);
      out.close();
  }
  </code></pre>
  
  <p>I can't understand why this servlet manages to serve one image and fails on others with a NullPointerException when the request parameter is correct. I have an impression like it is a concurrency issue because the Jsp displays arbitrary image.</p>
  
  <p>Any help please?</p>
  
  <p>Here is the DAO:</p>
  
  <pre><code>public Image getImageById(int imageId) {
          String query = "SELECT * FROM images WHERE imageId=?";
          Image img = null;
          PreparedStatement ps = null;
          ResultSet rs = null;
          try {
              connection = ConnectionManager.getConnection();
              ps = connection.prepareStatement(query);
              ps.setInt(1, imageId);
              rs = ps.executeQuery();
              if (rs.next()) {
                  img = new Image();
                  img.setImageId(rs.getInt("imageId")); //NPE thrown here
                  img.setImagePath(rs.getString("imagePath"));
                  img.setImageType(rs.getString("imageType"));
                  ...
                  img.setDescription(rs.getString("description"));
                  img.setCreatedOn(rs.getTimestamp("createdOn"));
              }
          } catch (SQLException e) {
              e.printStackTrace();
          } finally {
              close(rs);
              close(ps);
              close(connection); //Removed this and problem disappears
          }
          return img;
      }
  </code></pre>
  
  <p>Solved!!
  It is weird but I just avoided closing the connection to the database and all my images are displaying correctly with no errors. I guess closing the connection after every single DB access is problematic as it could not close at the exact time you want it to but maybe in the middle of another call to the DB. Now I wonder, not closing the connection at all is problematic too. What to do in that case?</p>