一聚教程网:一个值得你收藏的教程网站

热门教程

Java如何实现茶叶售卖商城系统 Java实现茶叶售卖商城系统代码

时间:2022-06-29 02:01:38 编辑:袖梨 来源:一聚教程网

Java如何实现茶叶售卖商城系统?本篇文章小编给大家分享一下Java实现茶叶售卖商城系统代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

前言

这是一个应用SSM框架的项目,前端页面整洁清晰。该系统有两个角色,一个是普通用户,另一个是管理员。

普通用户具有注册、登录、查看商品、添加购物车、添加商品收藏、下订单、商品评价、用户地址管理等等功能。

管理员具有登录、管理用户信息、管理商品信息、管理商品活动信息、管理订单信息、管理用户评论信息的等等功能。

应用技术:Jsp + SSM + EasyUi

运行环境:eclipse/IDEA + MySQL5.7 + Tomcat8.5 + JDK1.8

实现效果

主要代码

用户管理控制层

@WebServlet("/frontstage_userServlet")
public class UserServlet extends HttpServlet {
	UserService service = new UserService();
 
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action");
 
		switch (action) {
		case "login":
			login(req, resp);
			break;
		case "checkUserNameRepeat":
			checkUserNameRepeat(req, resp);
			break;
		case "register":
			register(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		case "checkOldPassword":
			checkOldPassword(req, resp);
			break;
		case "modifyPassword":
			modifyPassword(req, resp);
			break;
		}
	}
 
	public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式			
		resp.setContentType("text/html;charset=utf-8");
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String captcha = req.getParameter("captcha");
		if (captcha != null) {
			captcha = captcha.toUpperCase();
		}
 
		// 先进行验证码验证
		String checkcode = (String) req.getSession().getAttribute("checkcode_session");
 
		try {
			// 将用户输入的验证码和 系统验证对比
			if (checkcode.equals(captcha)) {
				User user = service.getUserByNameAndPassword(userName, password);
				if (user != null) {
					HttpSession session = req.getSession();
					session.setAttribute("user", user);
					JSONResult ok = JSONResult.ok();
					resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
				} else {
					// 将错误信息封装在结果集中
					JSONResult result = JSONResult.errorMsg("用户名或密码错误,请重试");
					// 以json的形式返回给前端
					resp.getWriter().println(JsonUtil.javaObjectToJson(result));
				}
			} else {
				// 将错误信息封装在结果集中
				JSONResult result = JSONResult.errorMsg("验证码输入错误,请重试");
				// 以json的形式返回给前端
				resp.getWriter().println(JsonUtil.javaObjectToJson(result));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	/**
	 * 用户名重名检测
	 * 
	 * @param req
	 * @param resp
	 */
	public void checkUserNameRepeat(HttpServletRequest req, HttpServletResponse resp) {
		String userName = req.getParameter("userName");
 
		if (userName != null) {
			int d = service.checkUserNameRepeat(userName);
			try {
				if (d > 0) {
					JSONResult error = JSONResult.errorMsg("");
					resp.getWriter().println(JsonUtil.javaObjectToJson(error));
				} else {
					JSONResult ok = JSONResult.ok();
					resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
 
	/**
	 * 用户注册
	 * 
	 * @param req
	 * @param resp
	 * @throws IOException 
	 */
	public void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式			
		resp.setContentType("text/html;charset=utf-8");
		User user = new User();
		user.setUserName(req.getParameter("userName"));
		user.setPassword(req.getParameter("password"));
		user.setEmail(req.getParameter("email"));
		user.setPhone(req.getParameter("phoneNum"));
		user.setRole("ordinaryUser");
		int d = service.addUser(user);
		try {
			if (d > 0) {
				JSONResult ok = JSONResult.ok();
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			} else {
				JSONResult error = JSONResult.errorMsg("注册失败");
				resp.getWriter().println(JsonUtil.javaObjectToJson(error));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	/**
	 * 退出登录
	 */
	public void logout(HttpServletRequest req, HttpServletResponse resp) {
		req.getSession().removeAttribute("user");
		try {
			resp.sendRedirect("index.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
	/**
	 * 检查旧密码是否正确 ,在修改密码操作时使用
	 * @throws IOException 
	 */
	public void checkOldPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式			
		resp.setContentType("text/html;charset=utf-8");
		User user = (User) req.getSession().getAttribute("user");		
		String oldPassword = service.getPasswordById(user.getId());
		System.out.println("旧密码:"+oldPassword);
		String password = req.getParameter("password");
		System.out.println("旧密码验证:"+password);
		try {
			if (!oldPassword.equals(password)) {
				JSONResult errorMsg = JSONResult.errorMsg("原始密码错误,请重新输入");
				resp.getWriter().println(JsonUtil.javaObjectToJson(errorMsg));
			} else {
				JSONResult ok = JSONResult.ok();
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
 
	}
 
	/**
	 * 修改密码
	 * @throws IOException 
	 */
	public void modifyPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式			
		resp.setContentType("text/html;charset=utf-8");
 
		String password = req.getParameter("newPassword");
		System.out.println("新密码:"+password);
		User user = (User) req.getSession().getAttribute("user");
		int id = user.getId();
		System.out.println("旧密码id"+id);
		int d = service.updatePasswordById(id, password);
		try {
			if (d > 0) {
				JSONResult ok = JSONResult.ok();
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			} else {
			  JSONResult errorMsg = JSONResult.errorMsg("修改失败,请重试");
			  resp.getWriter().println(JsonUtil.javaObjectToJson(errorMsg));
			}
 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
}

商品管理服务类

@WebServlet("/frontstage_goodsServlet")
public class GoodsServlet extends HttpServlet{
	GoodsService service = new GoodsService();
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action");
		switch(action) {
			case "findAllGoods":findAllGoods(req,resp);break;
			case "findGoodsById":findGoodsById(req,resp);break;
			case "findGoodsByType":findGoodsByType(req,resp);break;
		}
	}
	
	
	/**
	 * 获取所有商品列表
	 * @param req
	 * @param resp
	 */
	public void findAllGoods(HttpServletRequest req, HttpServletResponse resp) {
		Map> allGoods = service.getAllGoods();
		
		try {
			req.setAttribute("allGoods", allGoods);
			req.getRequestDispatcher("/main.jsp").forward(req, resp);
			return;
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 *根据类型获得商品列表 
	 * @param req
	 * @param resp
	 */
	public void findGoodsByType(HttpServletRequest req, HttpServletResponse resp) {
		String type = req.getParameter("type");
		String currentPage = req.getParameter("currentPage");
		
		PageBean pageBean = null;
		
		// 如果当前第几页currentPage 值为null,说明第一次跳转到此页面或者是要跳转到首页,则设定该值currentPage默认为1
		if(currentPage == null) {
			pageBean = service.getGoodsByType(type, 4, 1);
		}else {
			
			pageBean = service.getGoodsByType(type, 4, Integer.parseInt(currentPage));
		}
		
		try {
			req.setAttribute("pageBean", pageBean);
			req.setAttribute("type", type);
			
			req.getRequestDispatcher("/product.jsp").forward(req, resp);
 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 获取商品的详细信息
	 * @param req
	 * @param resp
	 */
	public void findGoodsById(HttpServletRequest req, HttpServletResponse resp) {
		int id =Integer.parseInt(req.getParameter("id"));
		
		Goods goodsInfo = service.getGoodsInfoById(id);
		
		try {
			req.setAttribute("pro", goodsInfo);
			req.getRequestDispatcher("/productInfo_user.jsp").forward(req, resp);
 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

订单控制层

@WebServlet("/backstage_ordersServlet")
public class OrdersServlet extends HttpServlet{
	
	OrdersService service = new OrdersService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action");
		System.out.println(action);
		switch(action) {
			case "getOrdersList":getOrdersList(req,resp);break;
			case "getOrdersListByName":getOrdersListByName(req,resp);break;
			case "deleteOrders":deleteOrders(req,resp);break;
			case "deleteAllOrders":deleteAllOrders(req,resp);break;
			case "toOrdersUpdatePage":toOrdersUpdatePage(req,resp);break;
			case "updateOrders":updateOrders(req,resp);break;
			case "fastbuy":sendOrder(req,resp);break;
		}
	}
 
	/* 
	 * 删除全部订单 
	 */
	private void deleteAllOrders(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
				req.setCharacterEncoding("utf-8");
				//设置响应编码格式			
				resp.setContentType("text/html;charset=utf-8");
		// TODO 自动生成的方法存根
		int d = service.deleteAllOrders();
		try {
			if(d>0) {
				JSONResult ok = JSONResult.ok();
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			}else {
				JSONResult errorMsg = JSONResult.errorMsg("删除失败,请重试");
				resp.getWriter().println(JsonUtil.javaObjectToJson(errorMsg));
			}			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	/**
	 * 提交订单
	 * @throws IOException 
	 */
	public void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
				req.setCharacterEncoding("utf-8");
				//设置响应编码格式			
				resp.setContentType("text/html;charset=utf-8");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			User user =(User) req.getSession().getAttribute("user");
			Orders order = new Orders();
			order.setNumber(order.getRandomString(10));
			order.setTime(df.format(new Date()));
			order.setName(req.getParameter("recipients"));
			order.setAddress(req.getParameter("address"));
			order.setPhone(req.getParameter("phone"));
			order.setAddress_label(req.getParameter("addressLabel"));
			order.setSex(req.getParameter("sex"));
			order.setUser(user.getUserName());
			order.setGoods_id(Integer.parseInt(req.getParameter("id")));
			order.setGoods_num(Integer.parseInt(req.getParameter("num")));
			order.setGoods_status(1);
			int d = service.addOrder(order);
				try {
					if(d>0) {
						JSONResult ok = JSONResult.ok();
						resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
					}else {
						JSONResult error = JSONResult.errorMsg("订单提交失败!");
						resp.getWriter().println(JsonUtil.javaObjectToJson(error));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}	
		
	}
 
	
	/**
	 * 向前端页面返回订单数据列表
	 */
	public void getOrdersList(HttpServletRequest req,HttpServletResponse resp) {
		List allOrders = service.getAllOrders();
		req.setAttribute("ordersList",allOrders);
		try {
			req.getRequestDispatcher("/backstage/tgls/ordersManage/orders_list.jsp").forward(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	
	/**
	 * 根据订单名查询商品(注意!类别没改)
	 * @param req
	 * @param resp
	 * @throws IOException 
	 */
	public void getOrdersListByName(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
				req.setCharacterEncoding("utf-8");
				//设置响应编码格式			
				resp.setContentType("text/html;charset=utf-8");
		String name = req.getParameter("OrdersUser");
		//String type = req.getParameter("type");
		
		Map parmas = new HashMap<>();
		parmas.put("name", name);
		//parmas.put("type", type);
		
		List list = service.getOrdersByName(parmas);
	
		try {
			if(list != null) {
				JSONResult ok = JSONResult.ok(list);
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			}else {
				JSONResult errorMsg = JSONResult.errorMsg("未获取到任何数据,请重试");
				resp.getWriter().println(JsonUtil.javaObjectToJson(errorMsg));
			}			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 修改订单时,获取此订单的全部数据,并返回至修改页面
	 */
	public void toOrdersUpdatePage(HttpServletRequest req,HttpServletResponse resp) {
		int id = Integer.parseInt(req.getParameter("id"));
		Orders orders = service.getOrdersInfoById(id);
		req.setAttribute("orders", orders);
		try {
			req.getRequestDispatcher("/backstage/tgls/ordersManage/orders_update.jsp").forward(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		
	}
	
	/**
	 *  修改订单
	 * @throws IOException 
	 */
	
	public void updateOrders(HttpServletRequest req,HttpServletResponse resp) throws IOException {
		//设置请求编码格式:
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式			
		resp.setContentType("text/html;charset=utf-8");
		Orders orders = new Orders();
		orders.setId(Integer.parseInt(req.getParameter("id")));
		orders.setNumber(req.getParameter("number"));
		orders.setUser(req.getParameter("user"));
		orders.setTime(req.getParameter("time"));
		orders.setName(req.getParameter("name"));
		orders.setSex(req.getParameter("sex"));
		orders.setAddress(req.getParameter("address"));
		orders.setPhone(req.getParameter("phone"));
		orders.setAddress_label(req.getParameter("address_label"));
		orders.setGoods_id(Integer.parseInt(req.getParameter("goods_id")));
		orders.setGoods_num(Integer.parseInt(req.getParameter("goods_num")));
		orders.setGoods_status(Integer.parseInt(req.getParameter("goods_status")));
		
		System.out.println(orders);
		int d = service.updateOrdersById(orders);
		System.out.println(d);
		try {
			if(d>0) {
				
				JSONResult ok = JSONResult.ok();
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			}else {
				JSONResult errorMsg = JSONResult.errorMsg("修改失败,请重试");
				resp.getWriter().println(JsonUtil.javaObjectToJson(errorMsg));
			}		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
 
	/**
	 * 删除订单
	 * @throws UnsupportedEncodingException 
	 */
	public void deleteOrders(HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException {
		//设置请求编码格式:
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式			
		resp.setContentType("text/html;charset=utf-8");
		int id = Integer.parseInt(req.getParameter("id"));
		
		int d = service.deleteOrdersById(id);
		try {
			if(d>0) {
				JSONResult ok = JSONResult.ok();
				resp.getWriter().println(JsonUtil.javaObjectToJson(ok));
			}else {
				JSONResult errorMsg = JSONResult.errorMsg("删除失败,请重试");
				resp.getWriter().println(JsonUtil.javaObjectToJson(errorMsg));
			}			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

热门栏目