——————————DescriptionStart——————————
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
注册、登录、注销
商品加入购物车,查看购物车,删除购物车中的商品和清空购物车
生成订单,查看历史订单及订单详情等







技术框架
JSP Servlet MySQL JDBC Tomcat
基于javaweb的JSP+Servlet简单购物车(java+jsp+servlet+mysql)
——————————CodeStart——————————
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| Random random=new Random(); for(int i=0;i<3;i++) { result+=random.nextInt(10); } return newDate+result; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/confirm.order".equals(path)){ confirm(req,resp); } if("/generate.order".equals(path)){ generate(req,resp); } if("/showOrders.order".equals(path)){ showOrders(req,resp); } if("/detail.order".equals(path)){ detail(req,resp); } }
protected void detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String OrderId=req.getParameter("id"); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM t_order where id='"+OrderId+"'"); try { while(rs.next()) { Order o=new Order(); o.setUsername(rs.getString("un")); o.setGoodsname(rs.getString("goodsname")); o.setNumber(rs.getInt("number")); o.setPrice(rs.getDouble("price")); o.setId(OrderId); order.add(o);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| User u=(User)req.getSession().getAttribute("user"); ResultSet rs = db.getData("SELECT distinct id FROM t_order where un='"+u.getUsername()+"' "); try { while(rs.next()) { String id=rs.getString("id"); OrderId.add(id); } } catch (SQLException e) { e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } req.setAttribute("OrderId", OrderId); req.getRequestDispatcher("historyOrders.jsp").forward(req, resp); OrderId.clear(); db.close(); }
protected void generate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id=getOrderIdByTime(); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); for(Cart c:cart_) { String sql="insert into t_order(id,un,goodsname,number,price) values('"+id+"','"+username+"','"+c.getGoodsname()+"',"+c.getNumber()+","+c.getPrice()+")"; db.setData(sql); } db.setData("DELETE FROM t_cart"); req.setAttribute("id", id); req.getRequestDispatcher("success.jsp").forward(req, resp); cart_.clear();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| try { while(rs.next()) { Cart c=new Cart(); c.setGoodsname(rs.getString(1)); c.setNumber(rs.getInt(2)); c.setPrice(rs.getDouble(3)); c.setUsername(username); cart.add(c); } } catch (SQLException e) { e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } req.setAttribute("cart", cart); req.getRequestDispatcher("order.jsp").forward(req, resp); cart_.clear(); cart_.addAll(cart); cart.clear(); db.close(); }
} package servlet;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public class CartServlet extends HttpServlet { public static List<Cart> cart=new ArrayList<Cart>(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/add.cart".equals(path)){ add(req,resp); } if("/show.cart".equals(path)){ show(req,resp); } if("/delete.cart".equals(path)){ delete(req,resp); } } protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String index=req.getParameter("index"); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM t_goods where goodsid="+index); String goodsname = ""; Double price = 0.0; try { if(rs.next()) { goodsname=rs.getString(2); price=rs.getDouble(3); } else { System.out.println("获取出错!!!"); } } catch (SQLException e) { e.printStackTrace(); } rs = db.getData("SELECT * FROM t_cart where goodsname='"+goodsname+"' and un='"+username+"'");
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| } } req.setAttribute("cart", cart); req.getRequestDispatcher("order.jsp").forward(req, resp); cart_.clear(); cart_.addAll(cart); cart.clear(); db.close(); }
} package servlet;
public class CartServlet extends HttpServlet { public static List<Cart> cart=new ArrayList<Cart>(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/add.cart".equals(path)){ add(req,resp); } if("/show.cart".equals(path)){ show(req,resp); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| resp.sendRedirect(req.getContextPath()+"/show.goods"); return; } req.setAttribute("msg", "用户名或密码错误!!!"); req.getRequestDispatcher("/login.jsp").forward(req, resp); } catch (SQLException e) { e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } db.close();
} protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session=req.getSession(); session.invalidate(); resp.sendRedirect(req.getContextPath()+"/show.goods"); } protected void check(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); if(u==null) { resp.sendRedirect(req.getContextPath()+"/login.jsp"); return; } String tag = req.getParameter("tag"); if("AddCart".equals(tag)) { String index=req.getParameter("index"); resp.sendRedirect(req.getContextPath()+"/add.cart?index="+index); } else{ resp.sendRedirect(req.getContextPath()+"/show.cart"); } } } package servlet;
|
——————————PayStart——————————
项目链接:
https://javayms.github.io?id=451321031406102al
https://javayms.pages.dev?id=451321031406102al