——————————DescriptionStart——————————
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明




基于javaweb的JSP+Servlet网上商城系统(java+jsp+servlert+mysql+ajax)
一、项目简述
功能: 主页显示热销商品;所有商品展示,可进行商品搜索;点 击商品进入商品详情页,显示库存,具有立即购买功能,可增减购买商品数量亦可手动输入(同时验证 库存),热销商品展示。立即购买进入确认订单页面,可选 择已经添加的地址,亦可新增地址。(同时验证库存),可 选择购买哪些商品,可删除不需要的商品。点击结算进入 确认订单页面,确认后提交订单,订单重复提交给予响 应,库存不足或商品下架给予响应
。后台管理:(修改密码 等),商品管理(商品批量添加、上下架、库存维护等), 订单管理。
前台购买商品后库存会相应减少
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe ( IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。
——————————CodeStart——————————
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| //设置请求编码格式: req.setCharacterEncoding("utf-8"); //设置响应编码格式 resp.setContentType("text/html;charset=utf-8"); String password = req.getParameter("newPassword"); User user =(User) req.getSession().getAttribute("user"); int id = user.getId(); 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(); } } /** * 成功修改密码后,清除session中的数据,并且重新跳转到登录页 * @param req * @param resp */ public void afterModifyPassword(HttpServletRequest req, HttpServletResponse resp) { req.getSession().removeAttribute("user"); try { //使用iframe时,要避免这种直接跳转的方式 // 此时只会让内嵌的iframe跳转到登录页,并非整个页面 // resp.sendRedirect("backstage/login.jsp"); PrintWriter writer = resp.getWriter(); writer.print("<html>"); writer.print("<script>"); writer.print("window.open('"+ req.getContextPath()+"/backstage/login.jsp','_top')"); writer.print("</script>"); writer.print("</html>"); } catch (IOException e) { e.printStackTrace(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class CheckImgServlet extends HttpServlet { static String baseNumLetter = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 120; int height = 30;
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.getGraphics(); graphics.setColor(getRandColor(200, 250)); graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.WHITE); graphics.drawRect(0, 0, width - 1, height - 1);
Graphics2D graphics2d = (Graphics2D) graphics; graphics2d.setFont(new Font("宋体", Font.BOLD, 28));
Random random = new Random();
StringBuffer sBuffer = new StringBuffer();
for(int i = 0; i < 4; i++){ int dot = random.nextInt(baseNumLetter.length()); sBuffer.append(baseNumLetter.charAt(dot)); }
String word = sBuffer.toString();
int x = 10; for (int i = 0; i < word.length(); i++) {
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| } catch (Exception e) { e.printStackTrace(); } } } package cn.gok.backstageManagement.web;
@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;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| resp.getWriter().println(JsonUtil.javaObjectToJson(ok)); } } catch (IOException e) { e.printStackTrace(); } }
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"); User user =(User) req.getSession().getAttribute("user"); int id = user.getId(); 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 afterModifyPassword(HttpServletRequest req, HttpServletResponse resp) { req.getSession().removeAttribute("user"); try {
PrintWriter writer = resp.getWriter();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| }else { JSONResult error = JSONResult.errorMsg("订单提交失败!"); resp.getWriter().println(JsonUtil.javaObjectToJson(error)); } } catch (Exception e) { e.printStackTrace(); } }
public void addGoods(HttpServletRequest req,HttpServletResponse resp) throws IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); FileUploadUtil upFile = new FileUploadUtil(); ServletContext servletContext = req.getSession().getServletContext(); List<FileItem> items= upFile.getRequsetFileItems(req,servletContext); Map<String, String> formData = new HashMap<>(); String savePath = new File(getServletContext().getRealPath("/")).getPath()+"/home/img";
try { for (FileItem file : items) { if(!file.isFormField()) { String fileSuffix = FileUploadUtil.getFileSuffix(file).toUpperCase(); if("JPG".equals(fileSuffix) || "PNG".equals(fileSuffix)) { String imgName = FileUploadUtil.getImgNewName(fileSuffix);
|
——————————PayStart——————————
项目链接:
https://javayms.github.io?id=251422292105200ey
https://javayms.pages.dev?id=251422292105200ey