0

I have a problem mixing JSP Servlet and c:forEach form... The problem is that whatever my id is, the HTML form will always send the first in the list, refered to ' name="id" '. Is there a solution maybe to get several name="id1", name="id2" ...etc in order to get the good one ?

Here is the code.

    <div id="client-table">
        <form action="gestion-commandes" method="POST">

            <table class="table table-striped">
                <thead class="thead-custom">
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">NOM</th>
                        <th scope="col">PRENOM</th>
                        <th scope="col">EMAIL</th>
                        <th scope="col">TEL</th>
                        <th scope="col">COMMANDES</th>
                    </tr>
                </thead>
                <tbody class="tbody-custom">

                    <c:forEach items="${clients}" var="client">
                        <tr>
                            <th scope="col">${client.id}<input name="id" value="${client.id}" hidden = "true"></th>
                            <th scope="col">${client.nom}</th>
                            <th scope="col">${client.prenom}</th>
                            <th scope="col">${client.email}</th>
                            <th scope="col">${client.tel}</th>
                            <th scope="col"><button type="submit">Commandes</button>
                            </th>
                        </tr>
                    </c:forEach>
                </tbody>                
            </table>

        </form>
    </div>

</div>

Thank you in advance, Joss

jozinho22
  • 459
  • 2
  • 7
  • 24

1 Answers1

0

When the parameter has multiple values, using request.getParameter("id") will return only the first parameter value.

You can get all values using request.getParameterValues instead of request.getParameter, for example:

String[] ids = request.getParameterValues("id");

But, if you only want to get one value, corresponding to the row you clicked, you can't use a hidden input, because all hidden inputs will be send with the submit.

The most straight forward way to get only the selected id is to pass it as a value of the button. Only the clicked button will be send with the submit.

<button type="submit" name="selected" value="${client.id}">Commandes</button>

And getting it on your servlet with:

String selectedId = request.getParametre("selected");

You will find more details on this answer of question marked as duplicate.

areus
  • 2,880
  • 2
  • 7
  • 17
  • I want to use only one id, the one I've clicked on. – jozinho22 May 10 '20 at 16:31
  • JSP servlet would read the first value, because they have all the same parameter ' name="id" ' – jozinho22 May 10 '20 at 16:32
  • Sorry, I didn't pay attention to the button inside the loop, so I misunderstood your question. I edited the answer, but you will find a more detailed explanation on the question marked as duplicate – areus May 10 '20 at 21:04
  • I solved it puting the
    inside the , created as form as object in the list, Instead of one
    – jozinho22 May 10 '20 at 22:43