📄 updateshopcartservlet.java
字号:
package shop;
import java.io.IOException;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public final class UpdateShopCartServlet extends HttpServlet{
public UpdateShopCartServlet(){
super();
}
public void init(ServletConfig config)throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
if(session==null){
System.out.println("no session");
System.exit(1);
}
Integer[] count = (Integer[])req.getAttribute("count");
Integer[] del = (Integer[])req.getAttribute("del");
ShopCart shopcart = new ShopCart();
Vector shopCartList=(Vector)session.getAttribute(Constants.SHOPCART_KEY);
/*
* 更新数量
*/
if(shopCartList!=null){
for(int i = 0; i < shopCartList.size() ;i++){
shopcart = (ShopCart)shopCartList.get(i);
shopcart.setCount(count[i].intValue());
shopCartList.set(i,shopcart);
}
/*
* 从购物车中删除
*/
for(int i = shopCartList.size()-1;i >=0 ;i--){
shopcart = (ShopCart)shopCartList.get(i);
for(int j = 0;j < del.length;j++){
if (shopcart.getProduct().getId()==del[j].intValue()){
shopCartList.remove(i);
}
}
}
session.setAttribute(Constants.SHOPCART_KEY,shopCartList);
}
resp.sendRedirect("basket_OK.jsp");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -