0

Ok, so I'm trying to add products to cart by introducing them in an ArrayList in servlet but the jstl "forEach" does not know how to iterate over it so im guessing im not doing a good job (I'm new to jsp-servlet)

I can add products but the previous ones are overwritten, so now I'm tryin to store them into an ArrayList which i store in a session variable from where i access it when i need to add another product.( i dont actually know if that's a good way to do it, but it seemed ok to me). Also this is the first question that i post so I'm sorry if i did a bad job.

@WebServlet(name = "mycart")
public class mycart extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        Product product = new Product();
        ShoppingCart items = new ShoppingCart();
        items.copyItems((ArrayList<Product>) session.getAttribute("sessionProducts"));
        session.removeAttribute("sessionProducts");
        String code = request.getParameter("code");

        if(items.equals(null)){
            items.addProduct(product.productById(code));
            request.setAttribute("listofproducts", items.getCart());
            session.setAttribute("sessionProducts", items.getCart());
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }
        else{
            session.setAttribute("sessionProducts", product.productById(code));
            request.setAttribute("listofproducts", product.productById(code));
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
<table border="1px solid">
            <tr>
                <th>Name</th>
                <th>Quantity</th>
                <th>Code</th>
                <th>Price</th>
            </tr>

            <tr>
                <c:forEach items="${listofproducts}" var="list">
                    <tr>
                        <td>${list.name}</td>
                        <td>${list.quantity}</td>
                        <td>${list.code}</td>
                        <td>${list.price}</td>
                    </tr>
                </c:forEach>
            </tr>

        </table>
public class ShoppingCart {

    public ArrayList<Product> items = new ArrayList<>();

    public void addProduct(Product product){
        this.items.add(product);
    }

    public ArrayList<Product> getCart(){
        return this.items;
    }

    public void copyItems(ArrayList<Product> items){
        this.items = items;
    }

}

All products have a "Add to cart" button, so i just want that when i click that button every product to be added to cart.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LAurek
  • 1
  • 2
  • Why are you absolutely positive that the line `request.setAttribute("listofproducts", items.getCart());` is executed and not `request.setAttribute("listofproducts", product.productById(code));`? – BalusC May 15 '19 at 12:05
  • Actually i tried to forward some random request parameter. it seems like when the first product is added to the list, the forEach statement cant really iterate the request variable. its fine if i dont use forEach tho. Like ${listofproducts.name} ${listofproducts.quantity} ${listofproducts.code} ${listofproducts.price} – LAurek May 15 '19 at 12:52
  • I didn't mean that .. the line `request.setAttribute("listofproducts", product.productById(code));` clearly doesn't set a **list** of products but a single product. How would you iterate over it? – BalusC May 15 '19 at 13:32
  • Yea I'm new to this. I figured that I store a single product but i didn't really knew how to do it the right way. I posted an answer. Do you think that's an ok approach? – LAurek May 15 '19 at 13:34

1 Answers1

0

I updated my servlet like that:

public class mycart extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        Product product=new Product();
        String code = request.getParameter("code");
        List<Product> list = (List<Product>) session.getAttribute("sessionProducts");
        if(code != null) {
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(product.productById(code));
            session.removeAttribute("sessionProducts");
            session.setAttribute("sessionProducts", list);
            request.setAttribute("listofproducts", list);
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }
        else{
            request.setAttribute("listofproducts", list);
            request.getRequestDispatcher("/cart.jsp").forward(request, response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

Now it works. The else part is for the case when you just want to check the products you previously added to your cart. I will do some more changes to add some another functionalities but i guess if someone searches for this problem, i hope it helped you

LAurek
  • 1
  • 2