title: How to retrieve image from database and display in JSP via Servlet?
body: <p>My ImageDAO looks like this:</p>
  
  <pre><code>public InputStream getPhotos(Long userid) throws 
    IllegalArgumentException, SQLException, ClassNotFoundException {
  
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultset = null;
    Database database = new Database();
    InputStream binaryStream = null;
  
    try {
  
      connection = database.openConnection();
      preparedStatement = connection.prepareStatement(SQL_GET_PHOTO);                  
      preparedStatement.setLong(1, userid);
      preparedStatement.executeUpdate();
  
      while(resultset.next()) {
        binaryStream = resultset.getBinaryStream(4);
      }
  
    } catch (SQLException e) {
        throw new SQLException(e);
    } finally {
        close(connection, preparedStatement, resultset);
    }
    return binaryStream;
  }
  </code></pre>
  
  <p>My ImageServlet looks like this: </p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
  
    // Getting user id from session
    HttpSession session = request.getSession(false);
    Long userid = (Long) session.getAttribute("user");    
  
    try {
  
        InputStream photoStream = imageDAO.getPhotos(userid);
  
        // Prepare streams.
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
  
        try {
  
        // Open streams
        input = new BufferedInputStream(photoStream, DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(),
                                                     DEFAULT_BUFFER_SIZE);
  
        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) &gt; 0) {
            output.write(buffer, 0, length);
        }
  
        } finally {
            output.close();
            input.close();
        }
  
        //Redirect it to profile page
        RequestDispatcher rd = request.getRequestDispatcher
                              ("/webplugin/jsp/profile/photos.jsp");
        rd.forward(request, response);
  
  
    } catch (Exception e) {
        e.printStackTrace();
    }
  
  }
  </code></pre>
  
  <p>How should my JSP image src look like</p>
  
  <pre><code>&lt;img src="What to put here"&gt;
  </code></pre>
  
  <p>Disclosure:</p>
  
  <p>The servlet code is copied from this link <a href="http://balusc.blogspot.com/2007/04/imageservlet.html" rel="nofollow">http://balusc.blogspot.com/2007/04/imageservlet.html</a></p>
  
  <p>Questions:</p>
  
  <ol>
  <li>How to retreive image in JSP from ImageServlet. Someone in Stackoverflow said to put
   <code>&lt;img src="URL to Servlet" /&gt;</code>. But I don't what it means.</li>
  <li>Is the above method correct way to retreive image from database? Or is there better way.</li>
  </ol>
  
  <p>EDIT: My Web.xml looks like this</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;servlet-name&gt;Photo Module&lt;/servlet-name&gt;
    &lt;servlet-class&gt;app.controllers.PhotoServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;Photo Module&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/Photos&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>