您好,登錄后才能下訂單哦!
小編給大家分享一下Java怎么實現仿天貓網上商城,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
功能:
前臺:
* 用戶模塊
* 分類模塊
* 商品模塊
* 購物車模塊
* 訂單模塊
后臺:
* 管理員模塊
* 分類管理模塊
* 商品管理模塊
* 訂單模塊
環境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
項目技術: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。
基礎信息操作: public abstract class BaseBackServlet extends HttpServlet { /* 添加 */ public abstract String add(HttpServletRequest request, HttpServletResponse response, Page page); /* 刪除 */ public abstract String delete(HttpServletRequest request, HttpServletResponse response, Page page); /* 更新 */ public abstract String update(HttpServletRequest request, HttpServletResponse response, Page page); /* 修改 */ public abstract String edit(HttpServletRequest request, HttpServletResponse response, Page page); /* 顯示 */ public abstract String list(HttpServletRequest request, HttpServletResponse response, Page page); protected CategoryDAO categoryDAO = new CategoryDAO(); protected OrderDAO orderDAO = new OrderDAO(); protected OrderItemDAO orderItemDAO = new OrderItemDAO(); protected ProductDAO productDAO = new ProductDAO(); protected ProductImageDAO productImageDAO = new ProductImageDAO(); protected PropertyDAO propertyDAO = new PropertyDAO(); protected PropertyValueDAO propertyValueDAO = new PropertyValueDAO(); protected ReviewDAO reviewDAO = new ReviewDAO(); protected UserDAO userDAO = new UserDAO(); @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try{ /* 獲取分頁信息 */ int start = 0; int count = 5; try{ start = Integer.parseInt(req.getParameter("page.start")); count = Integer.parseInt(req.getParameter("page.count")); } catch(Exception e) { } Page page = new Page(start, count); /* 借助反射,調用對應的方法 */ String method = (String)req.getAttribute("method"); Method m = this.getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class, Page.class); String redirect = m.invoke(this, req, resp, page).toString(); /* 根據方法的返回值,進行相應的客戶端、服務端跳轉,或僅輸出字符串 */ if(redirect.startsWith("@")){ resp.sendRedirect(redirect.substring(1)); } else if(redirect.startsWith("%")){ resp.getWriter().print(redirect.substring(1)); } else { req.getRequestDispatcher(redirect).forward(req, resp); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } public InputStream parseUpload(HttpServletRequest request, Map<String, String> params){ InputStream is = null; try{ DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); //設置上傳的文件大小限制為10M factory.setSizeThreshold(1024 * 1024); List items = upload.parseRequest(request); Iterator iter = items.iterator(); while(iter.hasNext()){ FileItem item = (FileItem) iter.next(); if(!item.isFormField()){ //獲取上傳文件的輸入流 is = item.getInputStream(); } else { String paramName = item.getFieldName(); String paramValue = item.getString(); paramValue = new String(paramValue.getBytes("ISO-8859-1"), "UTF-8"); params.put(paramName, paramValue); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return is; } }
用戶信息操作: public class UserServlet extends BaseBackServlet { @Override public String add(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 UserServlet add方法"); return null; } @Override public String delete(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 UserServlet delete方法"); int id = Integer.parseInt(request.getParameter("id")); userDAO.delete(id); return "@admin_user_list"; } @Override public String update(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 UserServlet update方法"); Map<String, String> params = new HashMap<>(); InputStream is = super.parseUpload(request, params); System.out.println(params); int id = Integer.parseInt(params.get("id")); String password = params.get("password"); User user = userDAO.get(id); user.setPassword(password); userDAO.update(user); return "@admin_user_list"; } @Override public String edit(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 UserServlet edit方法"); int id = Integer.parseInt(request.getParameter("id")); User u = userDAO.get(id); request.setAttribute("u", u); return "admin/editUser.jsp"; } @Override public String list(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 UserServlet list方法"); List<User> us = userDAO.list(page.getStart(), page.getCount()); int total = userDAO.getTotal(); page.setTotal(total); request.setAttribute("us", us); request.setAttribute("page", page); return "admin/listUser.jsp"; } }
以上是“Java怎么實現仿天貓網上商城”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。