-1
我是新來的ajax,就像它可以異步做事,而不必重新加載頁面。如果我想更新我的購物車總量而不必實際點擊「更新」按鈕,可以使用XMLHttpRequest對象完成。謝謝您的幫助。異步更新
<%
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");
}
%>
這個問題太寬泛了。通過教程來掌握基本概念。把你的項目放在一邊,制定一些基本的教程/例子。這是一個很好的起點:http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet/4113258#4113258 – BalusC 2011-05-13 19:59:42