-1

I'm new to ajax and like how it can do things asynchronously without having to reload the page. If I wanted to update my shopping cart total without having to actually hit the "update" button could it be done with the XMLHttpRequest object in this. Thanks for the help.

<% 
ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
if(cart==null){
response.sendRedirect("manageCart.jsp");
}else{
//We have the cart, let's update it
int itemCount = cart.getItemCount();
int quant = 0;
for(int i=itemCount-1; i>=0; i--){
    try{
        //User may have erased quantity or entered non-numeric value
        quant = new Integer(request.getParameter("item" + i)).intValue();
    }catch(NumberFormatException nfe){
        quant = 1;
    }
    //Get the next Item
    Item item = cart.getItem(i);
    //Set its quantity
    item.setQuantity(quant);
    if(request.getParameter("remove" + i)!=null){
        //Remove checkbox checked
        cart.removeItem(i);
    }

}
//Put the cart back in the session
session.setAttribute("cart",cart);
//go look at it!
response.sendRedirect("manageCart.jsp");
}
%>
Hedgehodge
  • 23
  • 4
  • This question is way too broad. Get yourself through a tutorial to grasp the basic concepts. Put your project aside and work out some basic tutorials/examples. This is a good starting point: http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet/4113258#4113258 – BalusC May 13 '11 at 19:59

1 Answers1

0

Ajax only handles the part after the "update order" is issued. If you want to avoid having to hit the update button you will need to trigger the Ajax request in a different manner, perhaps with a set-interval function of some sor?

hugomg
  • 68,213
  • 24
  • 160
  • 246