title: Dynamically display multiple images from database to jsp?
body: <p>Hello fellow stack overflowers.</p>
  
  <p>I am stuck on a certain portion of a project and am looking for some advice. </p>
  
  <p>I have a photo gallery page in jsp that is set up to display all the listings of photo albums and the photos within them based on the user's interest.  It is currently set up so that whatever 'photo' option is selected will be redirected to another jsp with the outputstream decoding on that particular page.  However, in order to see another photo or image, the user has to go 'back' to the photo galleries page and select another image for a new photo.</p>
  
  <p>Is there a way to set up the galleries page so that all the photos can be dynamically displayed on the page OR is there possibly a way put some direction on the display.jsp page so that the user can simply click from one photo to the next?????</p>
  
  <p>Code is below.</p>
  
  <pre><code>&lt;div id="subSlideshow"&gt;
                  &lt;table&gt;
                      &lt;tr&gt;
                          &lt;td id="subpageSlideshow"&gt;
                              &lt;table&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;
                                          &lt;h1 id="subpageTitle"&gt;Galleries&lt;/h1&gt;
                                              &lt;form action="gallery" method="get"&gt;
                                                  &lt;%try{
                                                    Connection conn = GalleriesConnection.connect();
                                                    Statement st = conn.createStatement();
                                                    String sql = "SHOW TABLES IN GALLERIES;";
                                                    ResultSet rs = st.executeQuery(sql);
                                                    while(rs.next()){%&gt;
                                                    &lt;input class="wineDiv" type="submit" name="gallery" value="&lt;%=rs.getString(1).toLowerCase() %&gt;" /&gt;
                                                    &lt;%} 
                                                    rs.close();
                                                    st.close();
                                                    GalleriesConnection.close(conn);
                                                    } 
  
                                                  catch(Exception e){
  
                                                    }%&gt;
                                              &lt;/form&gt;
                                          &lt;td&gt;
                                      &lt;/tr&gt;
                                  &lt;tr&gt;                                    
                                      &lt;td&gt;
                                          &lt;h1 id="subpageTitle"&gt;Images&lt;/h1&gt;
                                          &lt;form  action="pictures/display.jsp"&gt;
                                          &lt;%try{
                                            Connection conn = GalleriesConnection.connect();
                                            Statement st = conn.createStatement();
                                            String sql = "SELECT PHOTO_NAME FROM "+gallery+" ;";
                                            ResultSet rs = st.executeQuery(sql);
  
                                            while(rs.next()){   
                                            String photoName = rs.getString(1);
                                            %&gt;
                                              &lt;input class="wineDiv" type="submit" name="photo" value="&lt;%=photoName %&gt;" /&gt;  
                                          &lt;%   }
                                            rs.close();
                                            st.close();
                                            GalleriesConnection.close(conn);
  
                                          } catch(Exception e){                                               
                                          }%&gt;
                                          &lt;/form&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;    
  
  
                          &lt;/td&gt;
                          &lt;td id="subpageInfo"&gt;
                              &lt;h1 id="subpageTitle"&gt;Click on an image to see a larger view&lt;/h1&gt;
                              &lt;div id="slider"&gt;
                               &lt;script src ="js/slides.js" type="text/javascript"&gt;&lt;/script&gt;
                                  &lt;img id="1" src="pictures/winery_image1.jpg"  /&gt;
                                  &lt;img id="2" src="pictures/winery_image2.jpg" /&gt;
                                  &lt;img id="3" src="pictures/winery_image3.jpg" /&gt;
                                  &lt;img id="4" src="pictures/winery_image4.jpg" /&gt;
                                  &lt;img id="5" src="pictures/winery_image5.jpg" /&gt;
                              &lt;/div&gt;   
  
                              &lt;input class="previous" onclick="prev(); return false;" value="Previous Image" /&gt;
                              &lt;input class="next" onclick="next(); return false;" value="Next Image" /&gt;                                               
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/div&gt; 
  </code></pre>
  
  <p>All the action on the page is within this div.  The first connection command retrieves all the table names in the database and spits the table names out on submit buttons.  Additionally, the images currently listed in the subpageInfo are one of the desired spots on where I would like images from the database to be embedded.  They make a nice little fadeOut() fadeIn() transition when the next and previous buttons are selected.   </p>
  
  <p>The second connection gets all the names of the photos and puts them in the form of submits as well.  Selected photo names are sent to this page:</p>
  
  <pre><code>&lt;%@ page import="java.sql.*"%&gt;
  &lt;%@ page import="java.io.*"%&gt;
  
  &lt;% 
   byte[] imgData = null;
  %&gt;
  &lt;jsp:scriptlet&gt;
  String photo = request.getParameter("photo");
  &lt;/jsp:scriptlet&gt;
  &lt;%
  try {
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8889/GALLERIES","myusername","mypassword");
  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT PHOTO FROM PHOTOS WHERE PHOTO_NAME = '"+photo+"';");
  
  while (rs.next()) {
  Blob image = rs.getBlob(1);
  imgData = image.getBytes(1,(int)image.length());
  }  
  // display the image
  response.setContentType("image/png");
  OutputStream o = response.getOutputStream();
  o.write(imgData);
  o.flush();
  o.close();
  
  rs.close();
  stmt.close();
  con.close();
  } catch (Exception e) {
  out.println("Unable To Display image");
  out.println("Image Display Error=" + e.getMessage());
  return;
  } 
  %&gt;
  </code></pre>
  
  <p>This program does everything it is asked to do, but I am having some difficulty on making it more user friendly.  The desired effect is either embed the images in the subpageInfo div or to have some directives on the display.jsp page to go from one image to the next.  </p>
  
  <p>As always any and all help is greatly appreciated. </p>