(6)servlet包里新建ShowCartServlet
1
package net.hw.shop.servlet;
/**
* 功能:显示购物车控制程序
* 作者:liwenyu
* 日期:2019年12月16日
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.hw.shop.bean.Product;
import net.hw.shop.service.ProductService;
@WebServlet("/showCart")
public class ShowCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取商品类别标识符
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
// 获取session对象
HttpSession session = request.getSession();
// 获取购物车
LinkedHashMap<Integer, Integer> cart = (LinkedHashMap<Integer, Integer>) session.getAttribute("cart");
// 判断购物车是否为空
if (cart != null) {
// 定义购物表
List<HashMap<String, Object>> shoppingTable = new ArrayList<HashMap<String, Object>>();
// 创建商品服务对象
ProductService productService = new ProductService();
// 购物总金额
double totalPrice = 0.0;
// 遍历购物车
for (Integer id : cart.keySet()) {
// 获取商品对象
Product product = productService.findProductById(id);
// 生成购物表记录
HashMap<String, Object> shoppingItem = new HashMap<String, Object>();
shoppingItem.put("id", product.getId());
shoppingItem.put("name", product.getName());
shoppingItem.put("price", product.getPrice());
shoppingItem.put("amount", cart.get(id));
shoppingItem.put("sum", product.getPrice() * cart.get(id));
// 将购物表记录添加到购物表中
shoppingTable.add(shoppingItem);
// 累加购买总金额
totalPrice = totalPrice + (Double) shoppingItem.get("sum");
}
// 将购物表和购买总金额保存到session里
session.setAttribute("shoppingTable", shoppingTable);
session.setAttribute("totalPrice", totalPrice);
// 重定向到显示商品控制程序
response.sendRedirect(request.getContextPath() + "/showProduct?categoryId=" + categoryId);
} else {
System.out.println("购物车里没有类别号为[" + categoryId + "]的商品!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2
3
(7)servlet包里新建OperateCartServlet
1
1
package net.hw.shop.servlet;
/**
* 功能:操作购物车控制程序
* 作者:liwenyu
* 日期:2019年12月16日
*/
import java.io.IOException;
import java.util.LinkedHashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.hw.shop.service.ProductService;
@WebServlet("/operateCart")
public class OperateCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取商品标识符
int id = Integer.parseInt(request.getParameter("id"));
// 获取商品操作
String operation = request.getParameter("operation");
// 从session里获取购物车(键:商品标识符;值:购买数量)
LinkedHashMap<Integer, Integer> cart = (LinkedHashMap<Integer, Integer>) request.getSession()
.getAttribute("cart");
// 判断购物车是否为空
if (cart == null) {
// 创建购物车
cart = new LinkedHashMap<Integer, Integer>();
// 将购物车保存到session里,便于用户在不同页面访问购物车
request.getSession().setAttribute("cart", cart);
}
if (operation.equals("add")) {
// 将商品添加到购物车
if (cart.containsKey(id)) { // 该商品已购买过
// 购买数量增加1
cart.put(id, cart.get(id) + 1);
} else { // 该商品未曾购买
// 购买数量设置为1
cart.put(id, 1);
}
} else if (operation.equals("delete")){
// 将商品从购物车删除
if (cart.get(id) > 1) {
// 购买数量减少1
cart.put(id, cart.get(id) - 1);
} else {
// 从购物车里删除该商品
cart.remove(id);
}
}
// 创建商品服务对象
ProductService productService = new ProductService();
// 获取该商品类别标识符
int categoryId = productService.findProductById(id).getCategoryId();
// 重定向到显示购物车控制程序
response.sendRedirect(request.getContextPath() + "/showCart?categoryId=" + categoryId);
}
}
8、生成订单处理类(MakeOrderServlet)
1
package net.hw.shop.servlet;
/**
* 功能:生成订单控制程序
* 作者:li
* 日期:2019年12月16日
*/
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.hw.shop.bean.Order;
import net.hw.shop.bean.User;
import net.hw.shop.service.OrderService;
import net.hw.shop.service.UserService;
import net.hw.shop.service.OrderService;
import net.hw.shop.service.UserService;
@WebServlet("/makeOrder")
public class MakeOrderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取表单提交数据
String username = request.getParameter("username");
String telephone = request.getParameter("telephone");
Double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
String deliveryAddress = request.getParameter("deliveryAddress");
// 创建订单对象
Order order = new Order();
// 设置订单属性
order.setUsername(username);
order.setTotalPrice(totalPrice);
order.setTelephone(telephone);
order.setDeliveryAddress(deliveryAddress);
order.setOrderTime(new Date());
// 创建订单服务
OrderService orderService = new OrderService();
// 添加订单
orderService.addOrder(order);
// 获取最后一个订单
Order lastOrder = orderService.findLastOrder();
// 保存到session里
request.getSession().setAttribute("lastOrder", lastOrder);
// 重定向到显示订单页面
response.sendRedirect(request.getContextPath() + "/frontend/showOrder.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
9、支付处理类(PayServlet)
package net.hw.shop.servlet;
/**
* 功能:支付处理程序
* 作者:华卫
* 日期:2019年12月14日
*/
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/pay")
public class PayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
///
// 待做部分
// 设置session失效
request.getSession().invalidate();
// 重定向到登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
10、显示用户处理类(ShowUserServlet)
package net.hw.shop.servlet;
/**
* 功能:显示用户控制程序
* 作者:华卫
* 日期:2019年12月14日
*/
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.hw.shop.bean.User;
import net.hw.shop.service.UserService;
@WebServlet("/showUser")
public class ShowUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建用户服务对象
UserService userService = new UserService();
// 获取全部用户
List<User> users = userService.findAllUsers();
// 获取session对象
HttpSession session = request.getSession();
// 把用户列表以属性的方式保存到session里
session.setAttribute("users", users);
// 重定向到显示用户页面(showUser.jsp)
response.sendRedirect(request.getContextPath() + "/backend/showUser.jsp");
// 在控制台输出所有用户信息
for (User user: users) {
System.out.println(user);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}