Re: JSP beans retrieval with a for loop
Arne VajhHj wrote:
[example from a contact page - you should easily be able
to translate to books]
I'm trying this, but it doesn't work :(
########### INSIDE THE SERVLET:
List list = (List) request.getAttribute("allc");
if (list == null)
{
list = new ArrayList();
Bean b1 = new Bean("Vittorio", "123", "email1");
Bean b2 = new Bean("Arne", "345", "email2");
Bean b3 = new Bean("Barne", "567", "email3");
Bean b4 = new Bean("Carne", "789", "email4");
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
request.setAttribute("allc", list);
}
RequestDispatcher dispatcher =
request.getRequestDispatcher("/p4Solution/echart.jsp");
dispatcher.forward(request, response);
######## JSP PAGE
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%--
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Your Shopping Cart</h2>
<table border>
<tr>
<th>Navn</th>
<th>Telefon</th>
<th>Email</th>
</tr>
<c:forEach var="c" items="${allc}">
<tr>
<td><c:out value="${c.name}"/></td>
<td><c:out value="${c.phone}"/></td>
<td><c:out value="${c.email}"/></td>
<tr>
</c:forEach>
</table>
</body>
</html>
--
ciao
Vittorix