基于javaweb+jsp的网上商城系统(java+jsp+servlet+mysql+ajax)

基于javaweb+jsp的网上商城系统(java+jsp+servlet+mysql+ajax)

一、项目简述(+需求文档+PPT)
功能: 主页显示热销商品;所有商品展示,可进行商品搜索;点 击商品进入商品详情页,显示库存,具有立即购买和加入 购物车功能,可增减购买商品数量亦可手动输入(同时验证 库存),热销商品展示。立即购买进入确认订单页面,可选 择已经添加的地址,亦可新增地址。(同时验证库存),可 选择购买哪些商品,可删除不需要的商品。点击结算进入 确认订单页面,确认后提交订单,订单重复提交给予响 应,库存不足或商品下架给予响应。后台管理:(修改密码 等),商品管理(商品批量添加、上下架、库存维护等), 订单管理。

二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)

项目技术: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。

基于javaweb+jsp的网上商城系统(java+jsp+servlet+mysql+ajax)_第1张图片
基于javaweb+jsp的网上商城系统(java+jsp+servlet+mysql+ajax)_第2张图片
基于javaweb+jsp的网上商城系统(java+jsp+servlet+mysql+ajax)_第3张图片
基于javaweb+jsp的网上商城系统(java+jsp+servlet+mysql+ajax)_第4张图片

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

	 * 删除订单
	 * @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("删除失败,请重试");
商品服务类:
@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
				//设置响应编码格式			
				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 findAllGoods(HttpServletRequest req, HttpServletResponse resp) {
		Map<String, List<Goods>> 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();
		}
	}
	
	/**
	 *根据类型获得商品列表 
		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":
		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");
				//设置响应编码格式			
				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");
	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));
				}
				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");
		//设置响应编码格式			
		} 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");
					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();
		}
	}
	/**
	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()));
	}
	
	/**
	 *根据类型获得商品列表 
	 * @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));
			  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;
		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 {
		//设置请求编码格式:
		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"));
		//设置请求编码格式:
		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();
		}
				}	
		
	}
	
	/**
	 * 向前端页面返回订单数据列表
	 */
	public void getOrdersList(HttpServletRequest req,HttpServletResponse resp) {
		List<Orders> 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();
		}	
	}
	
	
	/**
		}
	}
	/* 
	 * 删除全部订单 
	 */
	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));
			}			
		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 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");
		//设置响应编码格式			
				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的形式返回给前端
			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");
	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;
		}
		} 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"));
	 * @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"));

你可能感兴趣的:(java,mysql)