0

I'm displaying my data in JSP using foreach from session variable.That session variable is Arraylist of products. I want to send the data from JSP to another JSP when user clicks on one product. How Can i achieve that>

<div class="row">
    <c:forEach items="${listProducts}" var="products">
    <div class="column">
        <div class="card">
        <form action="disp.jsp" method="GET">
                <h3>${products.pname}</h3>
                <p>price = &#8377;${products.price}</p>
                <p>Left out Stock: <b> ${products.quantity} </b> </p>
               <p> <input type="number" id="points" name="points" step="1" min="1" max="${products.quantity}"> </p>
                <p id="${products.pid}"></p>
                <p><button id="cartBtn">Add to Cart</button></p>
         </form>
         </div>
     </div>
    </c:forEach>
    
</div> 

How can send that specific product id and quantity which is clicked by user to another JSP. It is in for loop i cannot directly use request.getParameter("name").

Sasi Raj
  • 21
  • 5

1 Answers1

1

You can store ${products.pid} in some hidden inputs and as the details about products is already under form so the inputs which are there inside it will automatically get submitted to the other jsp page. i.e :

 <c:forEach items="${listProducts}" var="products">
    <div class="column">
        <div class="card">
           <form action="disp.jsp" method="GET">
                <h3>${products.pname}</h3>
                <p>price = &#8377;${products.price}</p>
                <p>Left out Stock: <b> ${products.quantity} </b> </p>
               <p> <input type="number" id="points" name="points" step="1" min="1" max="${products.quantity}"> </p>
               ///added hidden fields
                 <input type="hidden" name="id" value="${products.pid}"/>
                <p id="${products.pid}"></p>
               //add button type submit
                <p><button type="submit "id="cartBtn">Add to Cart</button></p>
         </form>
         </div>
     </div>
    </c:forEach>

And get value of id using request.getParameter("id") and for quantity write request.getParameter("points").

Swati
  • 28,069
  • 4
  • 21
  • 41