一、项目简述(+需求文档+PPT)
功能: 主页显示热销商品;所有商品展示,可进行商品搜索;点 击商品进入商品详情页,显示库存,具有立即购买和加入 购物车功能,可增减购买商品数量亦可手动输入(同时验证 库存),热销商品展示。立即购买进入确认订单页面,可选 择已经添加的地址,亦可新增地址。(同时验证库存),可 选择购买哪些商品,可删除不需要的商品。点击结算进入 确认订单页面,确认后提交订单,订单重复提交给予响 应,库存不足或商品下架给予响应。后台管理:(修改密码 等),商品管理(商品批量添加、上下架、库存维护等), 订单管理。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。
用户服务类:
@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<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();
}
}
/**
*根据类型获得商品列表
* @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<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();
}
}
/**
* 根据订单名查询商品(注意!类别没改)
* @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<String,String> parmas = new HashMap<>();
parmas.put("name", name);
//parmas.put("type", type);
List<Orders> 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();
}
}
}