📄 shoppingservlet.java
字号:
/**
* application name ShoppingServlet.java
* copyright Copyright 2008 东软 实训中心版权所有
* company neusoft
* time 2008-8-1
*
* @author 王洪雁
* @version ver 1.0
*/
package javaweb.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javaweb.dto.*;
public class ShoppingServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
// Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
ServletContext context = getServletContext();
HttpSession session = request.getSession();
ShopCart cart = (ShopCart) session.getAttribute("shopCart");
String action = request.getParameter("action");
if ("remove".equals(action)) {
String removeId = request.getParameter("removeId");
cart.removeProductFromCart(removeId);
} else if ("purchase".equals(action)) {
String[] productIds = request.getParameterValues("productId");
Map products = (Map) context.getAttribute("products");
if (cart == null) {
cart = new ShopCart();
session.setAttribute("shopCart", cart);
}
if (productIds == null) {
productIds = new String[0];
}
for (int i = 0; i < productIds.length; i++) {
Product product = (Product) products.get(productIds[i]);
cart.addProductToCart(product);
}
}
RequestDispatcher rd = request.getRequestDispatcher("/servlet/showcart");
rd.forward(request, response);
}
// Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -