There is a list of products on jsp , each has its own id . When you click the More button, the request from the item id should come to the servlet. But always zoachenie 1 comes.

 <form method="get" action="get_all_proposes/get_propose_by_id"> <c:forEach var="propose" items="${requestScope.allProposes}"> <ul> <li>Model: <c:out value="${propose.model}"/></li> <li>Mark: <c:out value="${propose.mark}"/></li> <li>Sold: <c:out value="${propose.sold}"/></li> <input name="pId" type="number" value="${propose.id}"> <input type="submit" value="More"> <hr> </ul> </c:forEach> </form> 

On the server, this is taken as:

 final int pId = Integer.valueOf(req.getParameter("pId")); 

Why always comes 1 ? How to fix it?

  • one
    the simplest option is to move the form tag inside the loop - each product will have its own form and when submitting, the data of a separate form will be sent. - Alex Chermenin

1 answer 1

let's say you have three objects

 <ul> ... <input name="pId" type="number" value="1"> <input type="submit" value="More"> </ul> <ul> ... <input name="pId" type="number" value="2"> <input type="submit" value="More"> </ul> <ul> ... <input name="pId" type="number" value="3"> <input type="submit" value="More"> </ul> 

And all this is in one form! When you click any of submit the values ​​of all input fly to the server, that is, the query string will look like this: get_all_proposes/get_propose_by_id?pId=1&pId=2&pId=3 . On the server, you most likely catch only the first one. I would advise you to fix

 <input type="submit" value="More"> 

on

 <a href="get_all_proposes/get_propose_by_id?pId=${propose.id}">More</a>