title: Display multiple images stored in database from servlet
body: <p>I am making a webpage using jsp and servlets. I am facing a problem when trying to display several images in jsp page. It take toooo long to display them and it as only a dozen images.</p>
  
  <p>THe code follows.</p>
  
  <pre><code>try (Connection con = DriverManager.getConnection(url, username, password)) {                                        
                  ps = con.prepareStatement("select foto from utilizadores where id = ?");
                  ps.setInt(1, Integer.parseInt(request.getParameter("id")));
                  rs = ps.executeQuery();
                  while(rs.next()){
  
  
  
                      imgBytes = rs.getBytes("foto");
                      response.setContentType("image/jpeg");
                      response.setContentLength(imgBytes.length);
                      response.getOutputStream().write(imgBytes);
  
                      response.getOutputStream().flush();
                      response.getOutputStream().close();
  
                  }
  </code></pre>
  
  <p>Them i am normally displaying the image in the jsp file.</p>
  
  <pre><code>&lt;img alt="foto de perfil" src="/uShare/socialImgServlet?id=&lt;%=Integer.parseInt(rs.getString("idUtilizador"))%&gt;"&gt;           
  </code></pre>