Skip to content
Advertisement

Java servlet request working correctly only for first of multiple auto generated html elements

unfortunately this is a project for school and most of the variables are in my native language, i’ll translate any if needed, but leave the code as is, just in case that’s somehow the problem.

I’m making a web app for a Catering Service, using Java servlets for my backend and JSP for my front end.

The session stores an Order object which has a HashMap<Product, Quantity>. On my Cart jsp foreach Product in the hashmap there’s a row in the css grid displayed on the screen.

<!-- Cart jsp snippet-->
                         <!--article             Order.getHashMap().keySet()-->
            <c:forEach var="stavka" items="${Narudzbina.getStavkeNarudzbine().keySet()}">
                <div class='korpa-stavka'>
                     <!-- article.getName() -->
                    <h5>${stavka.getNazivProizvoda()}</h5>
                    <input class="btn btn-light poeni-korpa" type="number" min="1" id="${stavka.getProizvodID()}" onchange="updateUrl(this)" value="${Narudzbina.getStavkeNarudzbine().get(stavka)}">
                    <div class="stavka-buttons">
                        <!-- Link is filled with a js script -->
                            <a href="" id="a${stavka.getProizvodID()}" class="btn btn-warning btn-stavka">Izmeni</a>
                            <!-- Link is static except for the ID, but no JS -->
                            <a href="Korpa?Zahtev=Izbrisi&Proizvod=${stavka.getProizvodID()}" class="btn btn-danger btn-stavka">IzbriĆĄi</a>
                    </div>
                </div>
                <p class='stavka-total'>Cena: ${stavka.getCenaPoPorciji() * Narudzbina.getStavkeNarudzbine().get(stavka)} RSD</p>
            </c:forEach>

This works as intended. One of the a tags href value is filled with an onchange call from the number input before it.

// Js for updating href of that a tag
function updateUrl(element){
           var link = document.getElementById("a" + element.id);
           link.setAttribute("href", "Korpa?Zahtev=Izmeni&Proizvod="+ element.id +"&Kolicina=" + element.value);  
}

That also works well, for every auto generated row from the hashtable. Clicking on the a button on any of them, as far as i can see, correctly calls the “Korpa” Controller, with good parameters, different, and correct ArticleID, different and correct Quantity for each of them.

Korpa servlet then packs the session Order object in a temporary Order, calls changeQuantity(article, newQuantity) on it, when that’s done packs the changed Order in session and reloads the cart page..

               // Order order = session order
               Narudzbina narudzbina = (Narudzbina)session.getAttribute("Narudzbina");
               //order.change quantity
               narudzbina.izmeniKolicinu(Integer.valueOf(request.getParameter("Proizvod")), Integer.valueOf(request.getParameter("Kolicina")));
               session.setAttribute("Narudzbina",narudzbina);
               //refreshes the Cart jsp 
               response.sendRedirect("Profil?User=" + session.getAttribute("User").toString() + "&View=Korpa");
               return;
               

The Order Model just changes the value in the hash map

             //changeQuantity     article ID      newQuantity
   public void izmeniKolicinu(int proizvodID, int novaKolicina) {
             //Order ord : hashMap.keySet()
        for (Proizvod prod : stavkeNarudzbine.keySet()) {
            //if ord.getArticleID == articleID
            if (prod.getProizvodID() == proizvodID) {
                stavkeNarudzbine.put(prod, novaKolicina);
            }
            break;
        }
    }

Now all of this works perfectly well for both changing quantity and deleting the article ( deleting is almost identical and there’s a switch statement in the Controller to check what to run, i ommited that since this is already a long post, and it’s very likely irrelevant )

But only for the first article drawn on the cart jsp screen. The other articles properly update their respective href‘s, and all properly call the servlet, the page refreshes, there’s no exceptions thrown, and the status of the request is 302 for both the 1st one ( that works ) and the rest. But the values do not update.

Clicking on any but the first generated link doesn’t update values

Also if i click on Remove on the first one that actually works, it gets removed, and the new first drawn one works now. Sorry for the rambling question, I’m just a student, really lost on this one, not really quite sure what the problem could be so I gave all the info i thought would be in any way helpful… P.S There’s no communication with the database anywhere in this process.

EDIT: Clarified problem

Advertisement

Answer

To have a proper answer here (the OP found it through the comments): the break needs to be within the if-condition

if (prod.getProizvodID() == proizvodID) {
    stavkeNarudzbine.put(prod, novaKolicina);
    break; // HERE
}
// NOT HERE: break;
Advertisement