您好,登錄后才能下訂單哦!
本篇內容介紹了“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<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(); } } }
“Java如何實現茶葉售賣商城系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。