0

I am developing "eShop" web application. I list all my products on jsp page ( with while loop) and each product has a button with value="Add to Card" and name=<%idArticle%>. When user clicks on button, the action of servlet have to add this product ( the clicked button) to the cart.

My problem is that servlet always took the first id of my products. So when ever I click on any button, the value is always the same and it is the value of the first id of the products on the list.

I have checked the functionality of jsp and it was working correctly.

So on jsp, when I click on a button, the idArticle is the id of the clicked button, but with servlet, the value is always the first id of the products on the list.

My jsp:

<form action="AddToCard" method="POST">
    <table border="1">
        <%
            ArticleCRUD articleCRUD = new ArticleCRUD();
            List data = articleCRUD.listArticles();
            Iterator iter = data.listIterator();
            while (iter.hasNext()) {
                Article art = (Article) iter.next();
                int idArticle = art.getIdArticle();

                String imagePath = null;
                imagePath = "Images/" + art.getIdArticle() + ".png";
        %>
        <tr>
            <td><img src=<%=imagePath%> width="50" height="50" alt="Picture is not available" <%=idArticle%> /> <br>
                Arcticle's name: <%=art.getNomArticle()%> <br> 
                Price: <%=art.getPriceArticle()%> <br> 
                Description: <%=art.getDescriptionArticle()%> <br>
                Categorie: <%=art.getCategorieArticle()%> <br> 
                Weight: <%=art.getWeightArticle()%> <br>
                Dimensions: <%=art.getDimensionsArticle()%> <br> 
                <input type="submit" value="Add To Card" name="<%=idArticle%>" /> 
                <input type="hidden" name="idUser" value=<%=idUser%> />                                                                                                                    
                <input type="hidden" name="idArticle" value="<%=idArticle%>" readonly />
            </td>
        </tr>

        <%
            }
        %>
    </table>
</form>

and this is the post methode of my servlet:

@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    if (request.getParameter("idArticle") != null) {
        System.out.println(request.getParameter("idArticle"));
    } else {
        System.out.println("Failed");
    }
}
Naman
  • 2,205
  • 2
  • 19
  • 32
Dhia1987
  • 1
  • 1

1 Answers1

0

and this is the post methode of my servlet:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (request.getParameter("idArticle") != null) {
        System.out.println(request.getParameter("idArticle"));
    } else {
        System.out.println("Failed");
    }
}
Peanut
  • 3,753
  • 3
  • 31
  • 45
Dhia1987
  • 1
  • 1