display iterator in jsp
I have the following code:
@Override
public Iterator retrieve() throws SQLException {
List<PasalBean> pasalObject = new ArrayList<PasalBean>();
try {
Class.forName(dbDriver);
con = DriverManager.getConnection(url);
ps = con开发者_如何学Go.createStatement();
rs = ps.executeQuery("select * from T_PASAL WHERE ID_PASAL = '" + id_pasal + "' ORDER BY ID_PASAL");
while (rs.next()) {
pasalObject.add(new PasalBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
}catch (Exception e) {
System.out.println("Error Data : " + e.getMessage());
}
return (Iterator) pasalObject;
}
How can I display it in JSP?
First, this code will fail. You cannot cast a List
to Iterator
. Make it return a List
.
Then, what you need in order to display it in a servlet is:
request.setAttribute("list", retrieve());
request.getRequestDispatcher("/path/to/jsp").forward();
And then in the JSP:
<c:forEach items="${list}" var="item">
${item.somePropertyOfTheBean}
</c:forEach>
It would be good to place the retrieve()
method in a separate, data-access class (DAO), and not directly in the servlet.
精彩评论