mpiekarz
mpiekarz

Reputation: 271

JSP foreach doesnt print the content of list

I have a small problem with foreach in jsp

I tried to do everything like in jstl/jsp - iterating over a vector of beans but I cant get anything printed on my screen

here is some code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Shop products</h1>
        <table>
            <c:forEach var="item" items="${products}">
                <tr>
                    <td>
                        <c:out value="${item.name}"/>
                    </td>
                <td>
                    <c:out value="${item.amount}"/>
                </td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

My servlet processRequest():

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    products = new LinkedList<ProductHandler>();
    products.add(new ProductHandler("A", 5));
    products.add(new ProductHandler("B", 10));
    products.add(new ProductHandler("C", 7));
    request.setAttribute("products", products);
    getServletContext().getRequestDispatcher("/JSP/shop.jsp").forward(request, response);
}

And ProductHandler:

public class ProductHandler {
    private String name;
    private int amount;

    public ProductHandler() { 
        name = null;
        amount = 0;
    }

    public ProductHandler(String name, int amount) {
        this.name = name;
        this.amount = amount;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

}

Id be very grateful for pointing out any mistake I made

Upvotes: 0

Views: 4331

Answers (1)

rickz
rickz

Reputation: 4474

Try adding this line to the top of shop.jsp

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

Upvotes: 4

Related Questions