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.
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.
The class that sets the HashMap with populated Order objects:
public HashMap getOrders() {
dbm = new DatabaseManager();
Statement stmt = null;
String sql = "SELECT * FROM orders";
HashMap<Integer, Order> orderMap = new HashMap<Integer, Order>();
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;
}
The Servlet that sets it as an attribute:
FormManager fm = new FormManager();
HashMap hm = fm.getOrders();
request.setAttribute("orders", hm);
The JSP attempting to populate the table:
<c:forEach var="orders" items="${ orders }">
<tr>
<%-- How to get the data from the order objects???? --%>
<td class="name">
<a href="">${ }</a>
</td>
<td>${ }</td>
<td>${ }</td>
<td>${ }</td>
</tr>
</c:forEach>
Any help is really appreciated. If I have approached this incorrectly, suggestions on the best way to do this would be great.