Dhruv Gairola
Dhruv Gairola

Reputation: 9182

Passing an ArrayList from a Servlet to JSP

my model returns an arraylist of strings to servlet in the form

ArrayList<String> currentCustomer = model.getAllCustomers();

i want to pass this arraylist from the servlet to the jsp page. how do i do this? below is what i tried

req.setAttribute("currentCustomer", currentCustomer);

and in the jsp page, i want to use JSTL to loop over each value and display it. how do i do that? its frustrating me to no end. ive scoured the web but to no avail. any help is greatly appreciated.

here is the jsp code

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>


<body>
    <div>
        <c:forEach var="customer" items="currentCustomer">
            ${customer}
        </c:forEach>
    </div>
</body>

Upvotes: 1

Views: 14814

Answers (3)

Dhruv Gairola
Dhruv Gairola

Reputation: 9182

its allrite guys, i solved the problem.. thanks for your help..

apparently the code i was using was outdated (thanks internet!) i was writing this on the header:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

while it should have been

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Upvotes: 2

Filip Spiridonov
Filip Spiridonov

Reputation: 36210

Let's make it work :)

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach var="customer" items="${currentCustomer}">
     <c:out value="${customer.name}" />
     <c:out value="${customer.age}" />
</c:forEach>

P.S. jsp:useBean is another way to go...

P.P.S. I also made a correction in the taglib import. That's one of these hard-visible mistakes when you can look on two different entries and think they are the same :)

Upvotes: 2

Stan Kurilin
Stan Kurilin

Reputation: 15792

It will be smt like

<c:forEach var="currentCustomer" items="${customers}">
     ${currentCustomer.name}
     ${currentCustomer.age}
</c:forEach>

Upvotes: 0

Related Questions