title: How to Populate a JSP table using JSP, Java Beans and Servlets
body: <p>I have a JSP application and I am trying to use a JSP to populate a table of customer's  wedding cake orders that has one order for each row. The way I have approached it so far has been to set each order in the MySQL table to an Order object and then add all of the order objects to a HashMap to be referenced in the JSP. </p>
  
  <p>Here's my question. So far, the Order objects are being created with the data in the database and placed in the HashMap. My only problem is I am confused about how I'm supposed to access the data in the JSP. I am using a c:forEach loop over the hashmap and am unsure how to access the attributes of the Order objects in the HashMap (such as the firstName or dueDate fields). I might have chosen a totally faulty approach as I'm relatively new to JSP. Here's my code.</p>
  
  <p>The class that sets the HashMap with populated Order objects:</p>
  
  <pre><code>public HashMap getOrders() {
      dbm = new DatabaseManager();
      Statement stmt = null;
      String sql = "SELECT * FROM orders";
      HashMap&lt;Integer, Order&gt; orderMap = new HashMap&lt;Integer, Order&gt;();
  
      dbm.setUrl("jdbc:mysql://localhost:3306/pattycakes");
      dbm.connect();
      try {
          stmt = dbm.getConn().createStatement();
          ResultSet rs = stmt.executeQuery(sql);
  
          rs.beforeFirst();
          int i = 0;
          while (rs.next()) {
              orderMap.put(i + 1,
                      new Order(rs.getString("first_name"), rs.getString("last_name"), rs.getString("phone"),
                              rs.getString("email"), rs.getString("due_date"), rs.getString("product_type"),
                              rs.getString("comments"), rs.getInt("id")));
              i++;
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } finally {
          dbm.disconnect(stmt, dbm.getConn());
      }
      return orderMap;
  }
  </code></pre>
  
  <p>The Servlet that sets it as an attribute: </p>
  
  <pre><code>FormManager fm = new FormManager();
  HashMap hm = fm.getOrders();
  request.setAttribute("orders", hm);
  </code></pre>
  
  <p>The JSP attempting to populate the table:</p>
  
  <pre><code>        &lt;c:forEach var="orders" items="${ orders }"&gt;
              &lt;tr&gt;
                &lt;%-- How to get the data from the order objects???? --%&gt;
                  &lt;td class="name"&gt;
                      &lt;a href=""&gt;${ }&lt;/a&gt;
                  &lt;/td&gt;
                  &lt;td&gt;${ }&lt;/td&gt;
                  &lt;td&gt;${ }&lt;/td&gt;
                  &lt;td&gt;${ }&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/c:forEach&gt;
  </code></pre>
  
  <p>Any help is really appreciated. If I have approached this incorrectly, suggestions on the best way to do this would be great.</p>