Java毕业设计实战之图片展览馆管理系统的实现

一、项目运行

环境配置:

Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)

项目技术:

Springboot+ SpringMVC + JPA+ Jsp + Html+ JavaScript + JQuery + Ajax + maven等等

订单服务:

@WebServlet("/order/OrderServlet")
public class OrderServlet extends HttpServlet {
	private OrderService service = new OrderServiceImpl();
	private ClientServiceImpl clientService=new ClientServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		String op = req.getParameter("op");// 得到传过来的请求
		// 生成订单
		if (op.equals("genOrder")) {
			genOrder(req, resp);
		}
		// 查看订单
		if (op.equals("findAllOrders")) {
			findAllOrders(req, resp);
		}
		// 管理员查看订单
		if (op.equals("findOrders")) {
			findOrders(req, resp);
		}
		// 发货
		if (op.equals("faHuo")) {
			faHuo(req, resp);
		}
		//删除订单
		if(op.equals("deletes")) {
			deletes(req,resp);
		}
	}

	private void deletes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String orderNumber = req.getParameter("orderNum");
		Order findOrderByNum = service.findOrderByNum(orderNumber);
		if(findOrderByNum.getItems().size()!=0) {
			resp.getWriter().print("<script>alert('该订单下有订单子项不可删除,请先删除订单子项!')</script>");
			resp.getWriter().print("<script>location.href='../admin/managerOrder.jsp'</script>");
			return;
		}
		try {
			service.deleteByNumber(orderNumber);
			resp.getWriter().print("<script>alert('删除成功!')</script>");
			resp.sendRedirect("../admin/managerOrder.jsp");
		}catch (Exception e) {
			resp.getWriter().print("<script>alert('该订单下有订单子项不可删除,请先删除订单子项!')</script>");
			return;
		}
	}

	private void faHuo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String ordernum = req.getParameter("ordernum");
		service.faHuo(ordernum);
		List<Order> orders = service.findOrders();
		HttpSession session = req.getSession();
		session.setAttribute("orders", orders);
		System.out.println(orders);
		resp.sendRedirect(req.getContextPath() + "/admin/managerOrder.jsp");
	}

	private void findOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Order> orders = service.findOrders();
		HttpSession session = req.getSession();
		session.setAttribute("orders", orders);
		req.getRequestDispatcher("/admin/managerOrder.jsp").forward(req, resp);
	}

	private void findAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		List<Order> orders = service.findUserOrders(user);
		req.setAttribute("orders", orders);
		req.getRequestDispatcher("/person/personOrder.jsp").forward(req, resp);
	}

	private void genOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		// 取出购物车信息
		// 取出购物项信息
		HttpSession session = req.getSession();
		Cart cart = (Cart) session.getAttribute("cart");
		User user = (User) session.getAttribute("user");
		if (cart == null) {
			session.setAttribute("message", "会话已经结束!");
			req.getRequestDispatcher("../message.jsp").forward(req, resp);
			return;
		}
		//先判断库存够不够!
		Book book = cart.getBook();
		String book_id = book.getBook_id();
		Book findBookById = clientService.findBookById(book_id);
		if(findBookById.getBook_kunumber()-cart.getTotalQuantity()<0) {
			session.setAttribute("message", "库存不足,无法购买!");
			session.removeAttribute("cart");
			req.getRequestDispatcher("../message.jsp").forward(req, resp);
			return;
		}
		Order order = new Order();
		order.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		String ordernum = genOrdernum();
		order.setOrdernum(ordernum);
		order.setQuantity(cart.getTotalQuantity());
		order.setMoney(cart.getTotalMoney());
		order.setUser(user);
		// 订单项
		List<Orderitem> oItems = new ArrayList<Orderitem>();
		for (Map.Entry<String, CartItem> me : cart.getItmes().entrySet()) {
			CartItem cItem = me.getValue();
			Orderitem oItem = new Orderitem();
			oItem.setId(genOrdernum());
			oItem.setBook(cItem.getBook());
			oItem.setPrice(cItem.getMoney());
			oItem.setQuantity(cItem.getQuantity());
			oItem.setOrdernum(ordernum);
			oItems.add(oItem);
		}
		// 建立订单项和订单的关系
		order.setItems(oItems);
		service.genOrder(order);
		req.setAttribute("order", order);
		req.getSession().removeAttribute("cart");
		req.removeAttribute("cart");
		req.getRequestDispatcher("/order.jsp").forward(req, resp);
	}

	// 生成订单号
	private String genOrdernum() {
		Date now = new Date();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		String s1 = df.format(now);
		return s1 + System.nanoTime();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

管理员登录服务:

@WebServlet("/admin/ManagerServlet")
public class ManagerServlet extends HttpServlet {
    private ManagerService service = new ManagerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String op = req.getParameter("op");// 得到传过来的请求
        // 管理员登陆
        if (op.equals("login")) {
            login(req, resp);
        }
        // 修改管理员资料
        if (op.equals("managerInformation")) {
            managerInformation(req, resp);
        }
        // 修改管理员登录密码
        if (op.equals("managerPassword")) {
            managerPassword(req, resp);
        }
        // 注销
        if (op.equals("layout")) {
            layout(req, resp);
        }
        // 添加书籍前先获取所有分类
        if (op.equals("addBookUI")) {
            addBookUI(req, resp);
        }
        // 添加书籍
        if (op.equals("addBook")) {
            try {
                addBook(req, resp);
            } catch (FileUploadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 添加书籍分类
        if (op.equals("addCategory")) {
            addCategory(req, resp);
        }
        // 文学艺术类书籍列表
        if (op.equals("category")) {
            getCategoryBook(req, resp);
        }
        // 编辑书籍信息前获取书籍的信息回显
        if (op.equals("editBookUI")) {
            editBookUI(req, resp);
        }
        // 编辑书籍
        if (op.equals("editBook")) {
            try {
                editBook(req, resp);
            } catch (FileUploadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 删除书籍
        if (op.equals("delBook")) {
            delBook(req, resp);
        }
        // 获取书籍分类列表
        if (op.equals("categoryList")) {
            categoryList(req, resp);
        }
        // 获得分类信息
        if (op.equals("editCategoryUI")) {
            editCategoryUI(req, resp);
        }
        // 修改书籍分类信息
        if (op.equals("editCategory")) {
            editCategory(req, resp);
        }
        // 删除书籍分类
        if (op.equals("delCategory")) {
            delCategory(req, resp);
        }
        // 用户信息管理
        if (op.equals("findUsers")) {
            findUsers(req, resp);
        }
        // 添加工作人员
        if (op.equals("addAdmin")) {
            addAdmin(req, resp);
        }
        // 书籍销售情况
        if (op.equals("sales")) {
            sales(req, resp);
        }
        //搜索
        if(op.equals("search")) {
        	search(req,resp);
        }
    }

    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	  String name = req.getParameter("search");
    	  if(name==null) {
    		  name="";
    	  }
    	  List<Book> findListByBookName = service.findListByBookName(name);
          List<Category> categoryList = service.findAllCategory();// 分类
          req.setAttribute("currentName", name);
          req.setAttribute("books", findListByBookName);
          req.setAttribute("categoryList", categoryList);
          req.getRequestDispatcher("/admin/booksList.jsp").forward(req, resp);
    }

    private void sales(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> sales = service.sales();
        req.setAttribute("sales", sales);
        req.getRequestDispatcher("/admin/sales.jsp").forward(req, resp);
    }

    private void addAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String tel = req.getParameter("tel");
        Administrator admin = new Administrator(username, password, name, sex, tel);
        service.addAdmin(admin);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>添加成功!</div>");
    }

    private void findUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list = service.findUsers();
        HttpSession session = req.getSession();
        session.setAttribute("users", list);
        req.getRequestDispatcher("/admin/managerUsers.jsp").forward(req, resp);
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if(username==null || username=="") {
        	  req.setAttribute("message", "请填写用户名");
        	  req.getRequestDispatcher("/admin/404.jsp").forward(req, resp);
        	  return;
        }
        if(password==null || password=="") {
        	 req.setAttribute("message", "请填写密码");
       	  req.getRequestDispatcher("/admin/404.jsp").forward(req, resp);
       	  return;
        }
        HttpSession session = req.getSession();
        Administrator admin = service.login(username, password);
        if (admin.getUsername() != null && admin.getUsername() != "") {
            req.setAttribute("message", "登陆成功");
            session.setAttribute("admin", admin);
            req.getRequestDispatcher("/admin/message.jsp").forward(req, resp);
        } else {
        	  req.setAttribute("message", "账号或密码错误!");
        	  req.getRequestDispatcher("/admin/404.jsp").forward(req, resp);
        }
    }

    private void managerInformation(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String tel = req.getParameter("tel");
        service.managerInformation(username, name, sex, tel);
        HttpSession session = req.getSession();
        Administrator admin = (Administrator) session.getAttribute("admin");
        admin.setName(name);
        admin.setSex(sex);
        admin.setTel(tel);
        session.setAttribute("admin", admin);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>修改成功!</div>");

    }
    //修改管理员密码
    private void managerPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        String oldPassword = req.getParameter("oldPassword");
        if(!password.equals(repassword)) {
        	resp.getWriter().print("<script>alert('两次密码不一致')</script>");
        	return;
        }
        Administrator admin = service.findAdminByUsername(username);
        if(!admin.getPassword().equals(oldPassword)) {
        	resp.getWriter().print("<script>alert('旧密码填写错误!')</script>");
        	return;
        }
        service.managerPassword(username, password);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>修改成功!</div>");
    }

    private void layout(HttpServletRequest req, HttpServletResponse resp) {
        try {
            HttpSession session = req.getSession();
            session.removeAttribute("admin");
            resp.sendRedirect("../admin/managerLogin.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addBookUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Category> categorys = service.findAllCategory();
        req.setAttribute("cs", categorys);
        req.getRequestDispatcher("/admin/addBook.jsp").forward(req, resp);

    }

    private void addBook(HttpServletRequest req, HttpServletResponse resp) throws FileUploadException, IOException {
        // 判断表单是不是multipart/form-data类型
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);
        if (!isMultipart) {
            throw new RuntimeException("表单上传类型有误!!");
        }
        // 创建工厂用来 解析请求内容
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 构建核心类对象
        ServletFileUpload sfu = new ServletFileUpload(factory);
        List<FileItem> items = new ArrayList<FileItem>();
        items = sfu.parseRequest(req);
        Book book = new Book();
        for (FileItem item : items) {
            if (item.isFormField()) {
                // 普通字段:把数据分装到book对象中
                processFormField(item, req, book);
            } else {
                // 上传字段:上传
                processUplodFiled(item, req, book);
            }
        }
        // 把书籍信息保存到数据库中
        service.addBook(book);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>添加成功!</div>");
    }

    // 处理文件上传
    private void processUplodFiled(FileItem item, HttpServletRequest req, Book book) {
        try {
            // 存放路径:不要放在web-inf中
            // 01.获取存放文件的真实目录
          //  String dirImages = getServletContext().getRealPath("/images");
            String dirImages =req.getServletContext().getRealPath("/")+"images/";
//            String dirImages = getServletContext().getRealPath("/img");
            // 02. 通过io存文件
            // 03. 生成文件名 (用户上传图片, 图片名可能重复)
            String FieldName = item.getFieldName();// 输入框的name值
            String name = item.getName();
            String fileType = name.substring(name.lastIndexOf(".") + 1);
            String fileName = UUID.randomUUID().toString();// 生成用不重复的文件名
            // 生成文件夹名
			Date time = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String strTime = simpleDateFormat.format(time);
            // path属性filename
            String path = strTime +File.separator + "books";// 存放到book对象中的路径
//            String path = "books";// 存放到book对象中
            String filename = fileName + "." + fileType;
            // fileDir:图片最终存在于fileDir
            File fileDir = new File(dirImages, path + File.separator + filename);
            // InputStream inputStream = item.getInputStream(); 从请求 对象中 通过流的方式
            // 把 上传的文件加载到 内存中 构建输出流
            File parentDir = new File(dirImages, path);// 父目录
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            book.setFilename(filename);
            book.setPath(path);

            InputStream inputStream = item.getInputStream();
            FileOutputStream fos = new FileOutputStream(fileDir);
            int len = 0;
            while ((len = inputStream.read()) != -1) {
                fos.write(len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 把Fielditem中的数据封装到book中
    private void processFormField(FileItem item, HttpServletRequest req, Book book) {
        try {
            // item每一个item对象代表一个输入框
            // 01. 获取input输入框的 name 的值 根据规范 输入框 的name的取值, 与 javabean 中的 属性名一致
            String inputName = item.getFieldName();
            String inputValue = item.getString("UTF-8");
            if (inputName.equals("categoryid")) {// 分类单独处理
                // 获取到选择的分类的id,根据这个id取到分类的信息
                String categoryid = item.getString();
                System.out.println("categoryid=" + categoryid);
                Category category = service.findCategoryById(categoryid);
                System.out.println(category);
                book.setCategory(category);
            } else {
                BeanUtils.setProperty(book, inputName, inputValue);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 添加书籍分类
    private void addCategory(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Category category = new Category();
            BeanUtils.populate(category, req.getParameterMap());
            String categoryName = category.getCategory_name();
            if(StringUtils.isNullOrEmpty(categoryName)) {
            	resp.getWriter().print("<script>alert('请填写分类名称')</script>");
            	resp.getWriter().print("<script>location.href='../admin/addCategory.jsp'</script>");
            	return;
            }
            service.addCategory(category);// 调用添加方法
            resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                    + "/img/duigou.png'/>添加成功!</div>");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getCategoryBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Book> books = service.getCategoryBook(req.getParameter("cid"));// 文学艺术类书籍
        List<Category> categoryList = service.findAllCategory();// 分类
        req.setAttribute("books", books);
        req.setAttribute("categoryList", categoryList);
        req.setAttribute("currentName", "");
        req.getRequestDispatcher("/admin/booksList.jsp").forward(req, resp);
    }

    private void editBookUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String book_id = req.getParameter("book_id");
        Book book = findBookById(book_id);
        List<Category> category = service.findAllCategory();
        HashMap map = new HashMap();
        map.put("book", book);
        map.put("category", category);
        req.setAttribute("map", map);
        req.getRequestDispatcher("/admin/editBook.jsp").forward(req, resp);
    }

    private void editBook(HttpServletRequest req, HttpServletResponse resp) throws FileUploadException, IOException {
        String book_id = req.getParameter("book_id");
        String book_name = req.getParameter("book_name");
        String book_author = req.getParameter("book_author");
        String book_press = req.getParameter("book_press");
        String book_desc = req.getParameter("book_desc");
        double book_price = Double.parseDouble(req.getParameter("book_price"));
        String book_kunumber = req.getParameter("book_kunumber");
        String categoryId = req.getParameter("categoryid");
        Category findCategoryById = service.findCategoryById(categoryId);
        service.editBook(book_id, book_name, book_author, book_press, book_desc, book_price, book_kunumber,findCategoryById);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>修改成功!</div>");
    }

    private void delBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String book_id = req.getParameter("book_id");
        service.delBook(book_id);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>删除成功!</div>");
    }

    private void categoryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Category> categoryList = service.findAllCategory();
        req.setAttribute("categoryList", categoryList);
        req.getRequestDispatcher("/admin/categorysList.jsp").forward(req, resp);
    }

    private void editCategoryUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Category category = service.findCategoryById(req.getParameter("category_id"));
        req.setAttribute("category", category);
        req.getRequestDispatcher("/admin/editCategory.jsp").forward(req, resp);
    }

    private void editCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Category category = new Category(req.getParameter("category_id"), req.getParameter("category_name"),
                req.getParameter("category_desc"));
        String categoryName = category.getCategory_name();
        if(StringUtils.isNullOrEmpty(categoryName)) {
        	resp.getWriter().print("<script>alert('请填写分类名称')</script>");
        	resp.getWriter().print("<script>location.href='../admin/editCategory.jsp'</script>");
        	return;
        }
        service.editCategory(category);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>修改成功!</div>");
    }

    private void delCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String category_id = req.getParameter("category_id");
        service.delCategory(category_id);
        resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
                + "/img/duigou.png'/>删除成功!</div>");
    }

    // 通过书籍id找到书籍信息
    private Book findBookById(String book_id) {
        return service.findBookById(book_id);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

收藏夹业务处理:

@WebServlet("/client/ClientServlet")
public class ClientServlet extends HttpServlet {
	private ClientService service = new ClientServiceImpl();
	private ManagerService managerService = new ManagerServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		String op = req.getParameter("op");// 得到传过来的请求
		if (op != null && !op.equals("")) {
			// 登录
			if (op.equals("login")) {
				login(req, resp);
			}
			// 注销
			if (op.equals("layout")) {
				layout(req, resp);
			}
			// 注册
			if (op.equals("register")) {
				register(req, resp);
			}
			// 文学艺术类书籍列表
			if (op.equals("category")) {
				getCategoryBook(req, resp);
			}

			// 个人信息修改
			if (op.equals("personInformation")) {
				personInformation(req, resp);
			}
			// 修改密码
			if (op.equals("personPassword")) {
				personPassword(req, resp);
			}
			// 搜索框
			if (op.equals("search")) {
				search(req, resp);
			}
			// 详情页面
			if (op.equals("particulars")) {
				particulars(req, resp);
			}
			// 添加购物车
			if (op.equals("addCart")) {
				addCart(req, resp);
			}
			// 删除购物车中的购物项
			if (op.equals("delItem")) {
				delItem(req, resp);
			}
			// 修改购物项数量
			if (op.equals("changeNum")) {
				changeNum(req, resp);
			}
			// 添加收藏夹
			if (op.equals("addfavorite")) {
				addfavorite(req, resp);
			}
			// 显示收藏夹
			if (op.equals("showfavorite")) {
				showfavorite(req, resp);
			}
			// 删除收藏夹
			if (op.equals("delFavorite")) {
				delFavorite(req, resp);
			}
			// 删除收藏夹
			if (op.equals("buyNow")) {
				buNow(req, resp);
			}

		}
	}

	private void delFavorite(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		String book_id = req.getParameter("book_id");
		service.delFavorite(book_id);
		HttpSession session = req.getSession();
		List<Favorite> lists = (List<Favorite>) session.getAttribute("favorite");
		Iterator<Favorite> iterator = lists.iterator();
		while (iterator.hasNext()) {
			Favorite favorite = iterator.next();
			if (book_id.equals(favorite.getBook().getBook_id())) {
				iterator.remove();// 使用迭代器的删除方法删除
			}
		}
		resp.sendRedirect(req.getContextPath() + "/favorite.jsp");
	}

	private void showfavorite(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		List<Favorite> favorites = service.findFavoriteByUserId(user);
		session.setAttribute("favorite", favorites);
		req.getRequestDispatcher("/favorite.jsp").forward(req, resp);
	}

	private void addfavorite(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		String user_id = user.getId();
		String book_id = req.getParameter("book_id");
		boolean isExit = service.findFavorite(user_id, book_id);
		if (isExit == false) {
			service.addfavorite(user_id, book_id);
		}
	}

	private void changeNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String num = req.getParameter("num");
		String book_id = req.getParameter("book_id");
		// 取出购物车
		Cart cart = (Cart) req.getSession().getAttribute("cart");
		CartItem item = cart.getItmes().get(book_id);
		item.setQuantity(Integer.parseInt(num));
		resp.sendRedirect(req.getContextPath() + "/showCart.jsp");

	}

	private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		HttpSession session = req.getSession();
		User user = service.login(username, password);
		if (user.getUsername() != null && user.getUsername() != "") {
			req.setAttribute("message", "登陆成功");
			session.setAttribute("user", user);
			req.getRequestDispatcher("/message.jsp").forward(req, resp);
		} else {
			req.setAttribute("message", "用户名或密码错误,请重新登录");
			req.getRequestDispatcher("/message.jsp").forward(req, resp);
		}
	}

	private void layout(HttpServletRequest req, HttpServletResponse resp) {
		try {
			HttpSession session = req.getSession();
			session.removeAttribute("user");// 获取session对象,从session中移除登陆信息
			resp.sendRedirect("../client/ClientServlet?op=category");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void register(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String username = req.getParameter("username");
			String password = req.getParameter("password");
			String name = req.getParameter("name");
			String sex = req.getParameter("sex");
			String tel = req.getParameter("tel");
			String address = req.getParameter("address");
			boolean isExist = false;// 判断是否存在该用户
			if (!username.equals("") && !password.equals("")) {
				isExist = service.register(username, password, name, sex, tel, address);
				if (isExist == true) {
					resp.getWriter().print("<script>alert('该用户已经注册,请直接登录')</script>");
					resp.getWriter().print("<script>location.href='../client/ClientServlet?op=category'</script>");
				} else {
					resp.getWriter().write("注册成功!");
					resp.getWriter().print("<script>location.href='../client/ClientServlet?op=category'</script>");
				}
			}else {
				resp.getWriter().print("<script>alert('请填写账号或密码')</script>");
				resp.getWriter().print("<script>location.href='../client/ClientServlet?op=category'</script>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getCategoryBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Book> books = service.getCategoryBook(req.getParameter("cid"));// 文学艺术类书籍
		req.setAttribute("books", books);
		List<Category> categoryList= managerService.findAllCategory();
		req.setAttribute("categoryList", categoryList);
		req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
	}

	private void personInformation(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = req.getParameter("username");
		String name = req.getParameter("name");
		String sex = req.getParameter("sex");
		String tel = req.getParameter("tel");
		String address = req.getParameter("address");
		service.personInformation(username, name, sex, tel, address);
		User user = (User) req.getSession().getAttribute("user");
		user.setUsername(username);
		user.setName(name);
		user.setSex(sex);
		user.setTel(tel);
		user.setAddress(address);
		req.getSession().setAttribute("user", user);
		resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
				+ "/img/duigou.png'/>修改成功!</div>");
	}

	private void personPassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String repassword = req.getParameter("repassword");
		String oldPassword = req.getParameter("oldPassword");
		if(!repassword.equals(password)) {
			resp.getWriter().print("<script>alert('两次密码不一致')</script>");
		}
		User user = service.findUserByUserName(username);
		if(user==null) {
			resp.getWriter().print("<script>alert('会话失效或未登录,请重新登录!')</script>");
			return;
		}
		if(!user.getPassword().equals(oldPassword)) {
			resp.getWriter().print("<script>alert('旧密码输入错误!')</script>");
			return;
		}
		service.personPassword(password, username);
		resp.getWriter().write("<div style='text-align: center;margin-top: 260px'><img src='" + req.getContextPath()
				+ "/img/duigou.png'/>修改成功!</div>");
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String search = req.getParameter("search");
		List<Book> searchmessage = service.search(search);
		req.setAttribute("books", searchmessage);
		req.setAttribute("name", search==null?"":search);
		req.getRequestDispatcher("/showBook.jsp").forward(req, resp);
	}

	private void particulars(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String book_id = req.getParameter("book_id");
		Book book = findBookById(book_id);
		req.setAttribute("book", book);
		req.getRequestDispatcher("/particulars.jsp").forward(req, resp);
	}

	// 通过书籍id找到书籍信息
	private Book findBookById(String book_id) {
		Book book = service.findBookById(book_id);
		return book;
	}

	private void addCart(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		String book_id = req.getParameter("book_id");
		Book book = findBookById(book_id);
		HttpSession session = req.getSession();
		Cart cart = (Cart) session.getAttribute("cart");
		if (cart == null) {
			cart = new Cart();
			cart.addBook(book);
			cart.setBook(book);
			session.setAttribute("cart", cart);
		}else {
			session.setAttribute("message", "购物车里有商品,请支付之后再来添加!");
			req.getRequestDispatcher("../message.jsp").forward(req, resp);
			return;
		}
		req.getRequestDispatcher("../showCart.jsp").forward(req, resp);
	}

	private void delItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String book_id = req.getParameter("book_id");
		Cart cart = (Cart) req.getSession().getAttribute("cart");
		cart.getItmes().remove(book_id);
		req.getSession().removeAttribute("cart");
		resp.sendRedirect(req.getContextPath() + "/showCart.jsp");
	}

	private void buNow(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		Cart cart1 = (Cart) req.getSession().getAttribute("cart");
		if(cart1==null) {
			Cart cart = new Cart();
			String book_id = req.getParameter("book_id");
			Book book = findBookById(book_id);
			cart.addBook(book);
			cart.setBook(book);
			req.getSession().setAttribute("cart", cart);
		}else {
			req.getSession().setAttribute("message", "购物车里有商品,请支付之后再来添加!");
			req.getRequestDispatcher("../message.jsp").forward(req, resp);
			return;
		}
		req.getRequestDispatcher("../showCart.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

到此这篇关于Java毕业设计实战之图片展览馆管理系统的实现的文章就介绍到这了,更多相关Java 图片展览馆管理系统内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Java毕业设计实战之二手书商城系统的实现

    一.项目简述 功能: 主页显示商品: 所有二手书商品展示,可进行商品搜索: 点击商品进入商品详情页,具有立即购买和加入购物车功能,可增减购买商品数量亦可手动输入,热销商品展示. 立即购买进入确认订单页面,可选择已经添加的地址,亦可新增地址. 可选择购买哪些商品,可删除不需要的商品. 点击结算进入确认订单页面,确认后提交订单.后台管理:(修改密码等),商品管理(商品批量添加.上下架等),订单管理. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(

  • Java毕业设计实战之食品溯源系统的实现

    一.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持). 项目技术: Springboot + Maven + mybatis+ Vue 等等组成,B/S模式 + Maven管理等等. 系统控制器: /** * 系统控制器 * @author yy * */ @RequestMapping("/system") @Co

  • Java毕业设计实战之在线网盘系统的实现

    一.项目简述 功能:用户的邮箱注册.验证码验证以及用户登录. 不需要注册账号,也可以上传满足条件的临时文件,但是只4小时内有效. 文件的管理,上传.下载.重命名.删除.查看统计数据.分类管理等. 文件夹的管理,创建.删除.重命名. 文件的分享,支持通过链接和二维码的分享方式等等,以及管理员对用户的管理等等. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持) 项目技术:

  • Java毕业设计实战之线上水果超市商城的实现

    主要技术实现:spring. springmvc. redis. springboot.  mybatis .session. jquery . md5 .bootstarp.js  tomcat..拦截器等. 主要功能实现: 前端:登录.注册.商品分类查看.浏览水果商品.订单管理.发表评论.收藏商品.购物车管理.个人订单管理查看.个人信息查看修改.地址管理等 后台管理员:后台登录.数据统计.系统版本信息等.管理员管理.角色管理.订单管理.通知公告管理.商品种类.和商品详情管理 主要功能截图如下

  • Java毕业设计实战项目之在线服装销售商城系统的实现流程

    一.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持). 项目技术: Spring + SpringBoot+ mybatis + Maven + Vue 等等组成,B/S模式 + Maven管理等等. 后台管理-用户页: /** * 后台管理-用户页 */ @Controller public class UserContro

  • Java毕业设计实战之校园一卡通系统的实现

    一.项目简述(+需求文档+PPT) 功能:卡管理,卡消费,卡充值,图书借阅,消费,记录,注销等等功能. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持) 项目技术: JSP + Servlet + html+ css + JavaScript + JQuery + Ajax 等等 用户管理操作控制层: /** * 用户管理操作 */ @Controller @Requ

  • Java毕业设计实战之仿小米电子产品售卖商城系统的实现

    项目描述:这是一个基于SpringBoot+Vue框架开发的仿小米电子产品售卖商城系统.首先,这是一个前后端分离的项目,代码简洁规范,注释说明详细.其次,这项目功能丰富,具有一个电子产品售卖商城系统该有的所有功能. 项目功能:此项目分为两个角色:普通用户和管理员.普通用户有登录注册.浏览商品信息.添加购物车.结算订单.查看个人信息.查看个人订单详情等等功能.管理员有管理所有商品信息.管理所有订单信息.管理所有用户信息.查看收益数据图表等等功能. 应用技术:SpringBoot + VueCli

  • Java毕业设计实战之药店信息管理系统的实现

    一.项目简述 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持) 项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等 药品相关的controller: /** * 药品相关的controller */ @Controller @Request

  • Java毕业设计实战之在线蛋糕销售商城的实现

    一.项目简述 功能: 主页显示商品: 所有蛋糕商品展示,可进行商品搜索: 点击商品进入商品详情页,具有立即购买功能,可增减购买商品数量亦可手动输入,热销商品展示. 立即购买进入确认订单页面,可选择已经添加的地址,亦可新增地址. 可选择购买哪些商品,可删除不需要的商品. 点击结算进入确认订单页面,确认后提交订单.后台管理:(修改密码等),商品管理(商品批量添加.上下架等),订单管理. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(Intelli

  • Java毕业设计实战之图片展览馆管理系统的实现

    一.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持) 项目技术: Springboot+ SpringMVC + JPA+ Jsp + Html+ JavaScript + JQuery + Ajax + maven等等 订单服务: @WebServlet("/order/OrderServlet") public class OrderServlet exte

  • Java毕业设计实战项目之仓库管理系统的实现流程

    基于SSM框架的仓库管理系统 功能: 系统操作权限管理.系统提供基本的登入登出功能,同时系统包含两个角色:系统超级管理员和普通管理员,超级管理员具有最高的操作权限,而普通管理员仅具有最基本的操作权限,而且仅能操作自己被指派的仓库. 请求URL鉴权.对于系统使用者登陆后进行操作发送请求的URL,后台会根据当前用户的角色判断是否拥有请求该URL的权限. 基础数据信息管理.对包括:货物信息.供应商信息.客户信息.仓库信息在内的基础数据信息进行管理,提供的操作有:添加.删除.修改.条件查询.导出为Exc

  • Java毕业设计实战之平行志愿管理系统的实现

    一.项目简述 本系统功能包括: 系统管理,招生计划,学生管理,录取结果,自动分配,调剂管理等等. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持). 项目技术: Springboot + Maven + mybatis+ Vue 等等组成,B/S模式 + Maven管理等等. 学生管理控制层: @RestControlle

  • Java毕业设计实战之教室预订管理系统的实现

    一.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持). 项目技术: Spring + SpringBoot+ mybatis + Maven + Vue 等等组成,B/S模式 + Maven管理等等. 用户管理控制器: /** * 用户管理控制器 */ @RequestMapping("/user/") @Contr

  • Java毕业设计实战之共享租车信息管理系统的实现

    基于servlet+jsp+jdbc的后台管理系统,包含5个模块:汽车账户部管理.租车账户部管理.汽车信息管理表.租车记录表.租车租聘表.功能完整,均能实现增删查改. 运行环境: jdk8+tomcat8.5+mysql5.7+Eclipse 项目技术: servlet+jsp+jdbc+easyui+jquery       主页登陆注册业务: public class ForeServlet extends BaseForeServlet { /** * 显示主页 * * @param re

  • Java毕业设计实战之养老院管理系统的实现

    运行环境: JDK1.8.tomcat8.eclipse.mysql5.6.Navicat 功能实现: 用户: 用户名,登录密码,姓名,性别,出生日期,用户照片,联系电话,邮箱,家庭地址,注册时间 老人: 老人编号,姓名,性别,年龄,老人照片,老人介绍,登记用户,登记时间 房间类型: 房间类型id,房间类型名称 房间: 房间编号,房间类型,房间名称,房间主图,房间价格,房间详情,房间状态 订单: 订单编号,入住房间,入住老人,入住日期,入住时间,订单总金额,订单状态,订单费用明细,订单时间 老人

  • Java毕业设计实战之健身俱乐部管理系统的实现

    项目介绍: 基于jsp+mysql+Spring+mybatis的SSM健身房管理系统 运行环境: jdk 1.8 IDE环境: Eclipse,Myeclipse,IDEA都可以 tomcat环境: Tomcat 7.x,8.x,9.x版本均可,理论上Tomcat版本不是太老都可以. 硬件环境: windows 7/8/10 1G内存以上 主要功能说明: 管理员角色包含以下功能:登录页面,管理员首页,会员增删改查,教练增删改查,运动器材管理等功能. 用户角色包含以下功能:用户登录页面,用户首页

  • Java毕业设计实战之工作管理系统的实现

    前台用户和后台管理员两种角色: 前台用户功能有:发布兼职.发布帖子.查看公告.个人中心.投诉等. 后台管理员功能有:用户管理.兼职管理.帖子管理.聊天管理.广告管理.投诉管理.收藏管理.系统管理等. 运行环境:jdk1.8.tomcat7.0\8.5.Eclipse.Mysql5.x. 后台角色管理控制器: /** * 后台角色管理控制器 * @author yy * */ @RequestMapping("/admin/role") @Controller public class

  • Java毕业设计实战之财务预算管理系统的实现

    一.项目简述 功能包括:实现公司对项目的管理. 二.项目运行 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持) 项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等 用户信息控制层: /** * 用户信息控制层 */ @Controller

随机推荐