Java项目:基于Jsp实现网上定餐系统

简介: Java项目:基于Jsp实现网上定餐系统

项目编号:BS-SC-001


本项目基于JSP+SERVLET+Durid连接池进行开发实现,数据库采用MYSQL数据库,开发工具为IDEA或ECLIPSE,前端用采用BootStrap开发实现。系统采用三层架构设计,MVC设计模式。系统功能完整,页面简洁大方,维护方便,适合做毕业设计使用。

具体系统功能展示如下:

前台页面功能:

20210125194732337.png

分类显示

image.png

餐品详情

image.png

添加购物车

image.png

个人订单管理

image.png

个人资料修改

image.png

系统留言

image.png

最近浏览功能

image.png

后台管理功能:

管理员登陆:  admin / admin

image.png

用户管理

image.png

分类管理

image.png

餐品管理

image.png

订单管理

image.png

留言管理

image.png

新闻管理

image.png

本系统是一款优秀的毕业设计系统,完美的实现了基于餐饮业务的网上订餐流程,功能强大,运行稳定,结构清晰,便于修改,适合做毕业设计使用。

部分核心代码:

package cn.jbit.easybuy.web;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.jbit.easybuy.biz.FacilityService;
import cn.jbit.easybuy.biz.OrderService;
import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.biz.impl.FacilityServiceImpl;
import cn.jbit.easybuy.biz.impl.OrderServiceImpl;
import cn.jbit.easybuy.biz.impl.ProductServiceImpl;
import cn.jbit.easybuy.entity.News;
import cn.jbit.easybuy.entity.Pager;
import cn.jbit.easybuy.entity.Product;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.entity.ShoppingCart;
import cn.jbit.easybuy.entity.User;
import cn.jbit.easybuy.util.ActionResult;
import cn.jbit.easybuy.util.Validator;
public class CartServlet extends HttpServlet {
  protected Map<String, ActionResult> viewMapping = new HashMap<String, ActionResult>();
  private ProductService productService;
  private FacilityService facilityService;
  private OrderService orderService;
  public void init() throws ServletException {
    productService = new ProductServiceImpl();
    facilityService = new FacilityServiceImpl();
    orderService = new OrderServiceImpl();
  }
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doPost(req, resp);
  }
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    createViewMapping();
    String actionIndicator = req.getParameter("action");
    String result = "";
    if (actionIndicator == null)
      actionIndicator = "list";
    if ("list".endsWith(actionIndicator)) {
      result = list(req);
    } else if ("add".endsWith(actionIndicator)) {
      result = add(req);
    } else if ("mod".endsWith(actionIndicator)) {
      result = mod(req);
    } else if ("remove".endsWith(actionIndicator)) {
      result = remove(req);
    } else if ("pay".endsWith(actionIndicator)) {
      result = pay(req);
    }
    toView(req, resp, result);
  }
  private String pay(HttpServletRequest request) {
    ShoppingCart cart = getCartFromSession(request);
    User user = getUserFromSession(request);
    if(user==null)
      return "login";
    orderService.payShoppingCart(cart, user);
    removeCartFromSession(request);
    return "paySuccess";
  }
  private void removeCartFromSession(HttpServletRequest request) {
    request.getSession().removeAttribute("cart");
  }
  private User getUserFromSession(HttpServletRequest request) {
    HttpSession session = request.getSession();
    return (User) session.getAttribute("loginUser");
  }
  private String add(HttpServletRequest request) {
    String id = request.getParameter("entityId");
    String quantityStr = request.getParameter("quantity");
    long quantity = 1;
    if (!Validator.isEmpty(quantityStr))
      quantity = Long.parseLong(quantityStr);
    Product product = productService.findById(id);
    ShoppingCart cart = getCartFromSession(request);
    cart.addItem(product, quantity);
    return "addSuccess";
  }
  private String mod(HttpServletRequest request) {
    String id = request.getParameter("entityId");
    String quantityStr = request.getParameter("quantity");
    long quantity = 1;
    if (!Validator.isEmpty(quantityStr))
      quantity = Long.parseLong(quantityStr);
    String indexStr = request.getParameter("index");
    ShoppingCart cart = getCartFromSession(request);
    cart.modifyQuantity(Integer.parseInt(indexStr), quantity);
    return "modSuccess";
  }
  private String remove(HttpServletRequest request) {
    String id = request.getParameter("entityId");
    String quantityStr = request.getParameter("quantity");
    long quantity = 1;
    if (!Validator.isEmpty(quantityStr))
      quantity = Long.parseLong(quantityStr);
    String indexStr = request.getParameter("index");
    ShoppingCart cart = getCartFromSession(request);
    cart.getItems().remove(Integer.parseInt(indexStr));
    return "removeSuccess";
  }
  private ShoppingCart getCartFromSession(HttpServletRequest request) {
    HttpSession session = request.getSession();
    ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
    if (cart == null) {
      cart = new ShoppingCart();
      session.setAttribute("cart", cart);
    }
    //取出当前用户的订单列表
    return cart;
  }
  private String list(HttpServletRequest request) {
    getCartFromSession(request);
    return "listSuccess";
  }
  private void prepareCategories(HttpServletRequest request) {
    List<ProductCategory> categories = productService
        .getProductCategories(null);
    request.setAttribute("categories", categories);
  }
  private void prepareNews(HttpServletRequest request) {
    List<News> allNews = facilityService.getAllNews(new Pager(10, 1));
    request.setAttribute("allNews", allNews);
  }
  protected void createViewMapping() {
    this.addMapping("listSuccess", "shopping.jsp");
    this.addMapping("paySuccess", "shopping-result.jsp");
    this.addMapping("addSuccess", "Cart", true);
    this.addMapping("removeSuccess", "Cart", true);
    this.addMapping("modSuccess", "Cart", true);
    this.addMapping("login", "login.jsp");
  }
  private void toView(HttpServletRequest req, HttpServletResponse resp,
      String result) throws IOException, ServletException {
    ActionResult dest = this.viewMapping.get(result);
    if (dest.isRedirect()) {
      resp.sendRedirect(dest.getViewName());
    } else {
      req.getRequestDispatcher(dest.getViewName()).forward(req, resp);
    }
  }
  protected void addMapping(String viewName, String url) {
    this.viewMapping.put(viewName, new ActionResult(url));
  }
  protected void addMapping(String viewName, String url, boolean isDirect) {
    this.viewMapping.put(viewName, new ActionResult(url, isDirect));
  }
}
package cn.jbit.easybuy.web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.biz.impl.ProductServiceImpl;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.util.ActionResult;
import cn.jbit.easybuy.util.Validator;
public class CategoryServlet extends HttpServlet {
  private ProductService productService;
  public void init() throws ServletException {
    productService = new ProductServiceImpl();
  }
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doPost(req, resp);
  }
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    String actionIndicator = req.getParameter("action");
    ActionResult result = new ActionResult("error");
    Validator validator = new Validator(Validator.toSingleParameters(req));
    if (actionIndicator == null)
      actionIndicator = "list";
    if ("read".endsWith(actionIndicator)) {
      result = read(req, validator);
    } else if ("list".endsWith(actionIndicator)) {
      result = list(req, validator);
    } else if ("create".endsWith(actionIndicator)) {
      result = create(req, validator);
    } else if ("delete".endsWith(actionIndicator)) {
      result = delete(req, validator);
    } else if ("save".endsWith(actionIndicator)) {
      boolean isEdit = true;
      String editIndicator = req.getParameter("entityId");
      if (Validator.isEmpty(editIndicator))
        isEdit = false;
      result = save(req, validator, isEdit);
    }
    if (!validator.hasErrors() && result.isRedirect()) {
      resp.sendRedirect(result.getViewName());
    } else {
      req.setAttribute("errors", validator.getErrors());
      req.getRequestDispatcher(result.getViewName()).forward(req, resp);
    }
  }
  public ActionResult read(HttpServletRequest request, Validator validator) {
    ProductCategory category = productService.findCategoryById(request
        .getParameter("entityId"));
    pupulateRequest(request, category);
    List<ProductCategory> categories = productService.getRootCategories();
    request.setAttribute("categories", categories);
    return new ActionResult("productClass-modify.jsp");
  }
  public ActionResult save(HttpServletRequest request, Validator validator,
      boolean isEdit) {
    String entityId = request.getParameter("entityId");
    checkInputErrors(request, validator);
    saveToDatabase(request, validator, isEdit);
    return new ActionResult("Category", true);
  }
  public ActionResult create(HttpServletRequest request, Validator validator) {
    List<ProductCategory> categories = productService.getRootCategories();
    request.setAttribute("categories", categories);
    request.setAttribute("parentId", 0);
    return new ActionResult("productClass-modify.jsp");
  }
  public ActionResult delete(HttpServletRequest request, Validator validator) {
    productService.deleteCategory(request.getParameter("entityId"));
    return new ActionResult("Category", true);
  }
  public ActionResult list(HttpServletRequest request, Validator validator) {
    List<ProductCategory> categories = productService
        .getProductCategories(null);
    request.setAttribute("categories", categories);
    return new ActionResult("productClass.jsp");
  }
  private void saveToDatabase(HttpServletRequest request,
      Validator validator, boolean isEdit) {
    if (!validator.hasErrors()) {
      ProductCategory productCategory;
      if (!isEdit) {
        productCategory = new ProductCategory();
        populateEntity(request, productCategory);
        productCategory.setParentId(Long.parseLong(request
            .getParameter("parentId")));
        productService.saveCategory(productCategory);
      } else {
        productCategory = productService.findCategoryById(request
            .getParameter("entityId"));
        Long parentId = Long
            .parseLong(request.getParameter("parentId"));
        populateEntity(request, productCategory);
        if (parentId == 0) {
          if (productCategory.getId().equals(
              productCategory.getParentId())) {
            // 说明是一级分类,父分类不能修改,只能改名字
            productService.updateCategoryName(productCategory);
          } else {
            // 二级分类修改为一级分类了,需要额外更新:
            // Product原先属于该二级分类的,全部更新一级为它,二级为空
            productCategory.setParentId(productCategory.getId());
            productService.updateCategory(productCategory,
                "Level2To1");
          }
        } else {
          if (!parentId.equals(productCategory.getParentId())) {
            // 二级分类修改了父分类,需要额外更新:
            // Product原先属于该二级分类的,全部更新一级为新的父分类
            productCategory.setParentId(parentId);
            productService.updateCategory(productCategory,
                "ModifyParent");
          } else {
            // 二级分类修改了名字
            productService.updateCategoryName(productCategory);
          }
        }
      }
    }
  }
  private void pupulateRequest(HttpServletRequest request,
      ProductCategory productCategory) {
    request
        .setAttribute("entityId", Long
            .toString(productCategory.getId()));
    request.setAttribute("name", productCategory.getName());
    request.setAttribute("parentId", (productCategory.getParentId()
        .equals(productCategory.getId())) ? 0 : productCategory
        .getParentId());
  }
  private void checkInputErrors(HttpServletRequest request,
      Validator validator) {
    validator.checkRequiredError(new String[] { "name" });
  }
  private void populateEntity(HttpServletRequest request,
      ProductCategory productCategory) {
    productCategory.setName(request.getParameter("name"));
  }
}
package cn.jbit.easybuy.web;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.jbit.easybuy.biz.FacilityService;
import cn.jbit.easybuy.biz.ProductService;
import cn.jbit.easybuy.biz.impl.FacilityServiceImpl;
import cn.jbit.easybuy.biz.impl.ProductServiceImpl;
import cn.jbit.easybuy.entity.Comment;
import cn.jbit.easybuy.entity.Pager;
import cn.jbit.easybuy.entity.ProductCategory;
import cn.jbit.easybuy.util.ActionResult;
import cn.jbit.easybuy.util.Validator;
public class CommentServlet extends HttpServlet {
  private FacilityService facilityService;
  private ProductService productService;
  public void init() throws ServletException {
    this.facilityService = new FacilityServiceImpl();
    this.productService = new ProductServiceImpl();
  }
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doPost(req, resp);
  }
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    String actionIndicator = req.getParameter("action");
    ActionResult result = new ActionResult("error");
    Validator validator = new Validator(Validator.toSingleParameters(req));
    if (actionIndicator == null)
      actionIndicator = "list";
    if ("read".endsWith(actionIndicator)) {
      result = read(req, validator);
    } else if ("list".endsWith(actionIndicator)) {
      result = list(req, validator);
    } else if ("delete".endsWith(actionIndicator)) {
      result = delete(req, validator);
    } else if ("save".endsWith(actionIndicator)) {
      boolean isEdit = true;
      String editIndicator = req.getParameter("entityId");
      if (Validator.isEmpty(editIndicator))
        isEdit = false;
      result = save(req, validator, isEdit);
    }
    if (!validator.hasErrors() && result.isRedirect()) {
      resp.sendRedirect(result.getViewName());
    } else {
      req.setAttribute("errors", validator.getErrors());
      req.getRequestDispatcher(result.getViewName()).forward(req, resp);
    }
  }
  public ActionResult read(HttpServletRequest request, Validator validator) {
    Comment comment = facilityService.findCommentById(request
        .getParameter("entityId"));
    pupulateRequest(request, comment);
    return new ActionResult("guestbook-modify.jsp");
  }
  public ActionResult save(HttpServletRequest request, Validator validator,
      boolean isEdit) {
    checkInputErrors(request, validator);
    saveToDatabase(request, validator, isEdit);
    return new ActionResult("GuestBook", true);
  }
  public ActionResult delete(HttpServletRequest request, Validator validator) {
    facilityService.deleteComment(request.getParameter("entityId"));
    return new ActionResult("GuestBook", true);
  }
  public ActionResult list(HttpServletRequest request, Validator validator) {
    String page = request.getParameter("page");
    int pageNo = 1;
    if (!Validator.isEmpty(page))
      pageNo = Integer.parseInt(page);
    long rowCount = facilityService.getCommentRowCount();
    Pager pager = new Pager(rowCount, pageNo);
    List<Comment> comments = facilityService.getComments(pager);
    List<ProductCategory> categories = productService
        .getProductCategories(null);
    request.setAttribute("categories", categories);
    request.setAttribute("comments", comments);
    request.setAttribute("pager", pager);
    request.setAttribute("pageNo", pageNo);
    return new ActionResult("guestbook.jsp");
  }
  private void pupulateRequest(HttpServletRequest request, Comment comment) {
    request.setAttribute("entityId", Long.toString(comment.getId()));
    request.setAttribute("reply", comment.getReply());
    request.setAttribute("content", comment.getContent());
    request.setAttribute("nickName", comment.getNickName());
    request.setAttribute("replayTime", Validator.dateToString(comment
        .getReplyTime()));
  }
  private void saveToDatabase(HttpServletRequest request,
      Validator validator, boolean isEdit) {
    if (!validator.hasErrors()) {
      Comment comment;
      if (!isEdit) {
        comment = new Comment();
        comment.setCreateTime(new Date());
        populateEntity(request, comment);
        facilityService.saveComment(comment);
      } else {
        comment = facilityService.findCommentById(request
            .getParameter("entityId"));
        if (!Validator.isEmpty(request.getParameter("reply"))) {
          comment.setReply(request.getParameter("reply"));
          comment.setReplyTime(new Date());
        }
        facilityService.updateComment(comment);
      }
    }
  }
  private void checkInputErrors(HttpServletRequest request,
      Validator validator) {
    validator.checkRequiredError(new String[] { "content", "nickName" });
  }
  private void populateEntity(HttpServletRequest request, Comment comment) {
    comment.setContent(request.getParameter("content"));
    comment.setNickName(request.getParameter("nickName"));
  }
}
相关文章
|
25天前
|
监控 Java API
如何使用Java语言快速开发一套智慧工地系统
使用Java开发智慧工地系统,采用Spring Cloud微服务架构和前后端分离设计,结合MySQL、MongoDB数据库及RESTful API,集成人脸识别、视频监控、设备与环境监测等功能模块,运用Spark/Flink处理大数据,ECharts/AntV G2实现数据可视化,确保系统安全与性能,采用敏捷开发模式,提供详尽文档与用户培训,支持云部署与容器化管理,快速构建高效、灵活的智慧工地解决方案。
|
7天前
|
NoSQL Java 关系型数据库
Liunx部署java项目Tomcat、Redis、Mysql教程
本文详细介绍了如何在 Linux 服务器上安装和配置 Tomcat、MySQL 和 Redis,并部署 Java 项目。通过这些步骤,您可以搭建一个高效稳定的 Java 应用运行环境。希望本文能为您在实际操作中提供有价值的参考。
60 26
|
19天前
|
XML Java 测试技术
从零开始学 Maven:简化 Java 项目的构建与管理
Maven 是一个由 Apache 软件基金会开发的项目管理和构建自动化工具。它主要用在 Java 项目中,但也可以用于其他类型的项目。
31 1
从零开始学 Maven:简化 Java 项目的构建与管理
|
16天前
|
设计模式 消息中间件 搜索推荐
Java 设计模式——观察者模式:从优衣库不使用新疆棉事件看系统的动态响应
【11月更文挑战第17天】观察者模式是一种行为设计模式,定义了一对多的依赖关系,使多个观察者对象能直接监听并响应某一主题对象的状态变化。本文介绍了观察者模式的基本概念、商业系统中的应用实例,如优衣库事件中各相关方的动态响应,以及模式的优势和实际系统设计中的应用建议,包括事件驱动架构和消息队列的使用。
|
18天前
|
Java
Java项目中高精度数值计算:为何BigDecimal优于Double
在Java项目开发中,涉及金额计算、面积计算等高精度数值操作时,应选择 `BigDecimal` 而非 `Double`。`BigDecimal` 提供任意精度的小数运算、多种舍入模式和良好的可读性,确保计算结果的准确性和可靠性。例如,在金额计算中,`BigDecimal` 可以精确到小数点后两位,而 `Double` 可能因精度问题导致结果不准确。
|
28天前
|
Java Android开发
Eclipse 创建 Java 项目
Eclipse 创建 Java 项目
39 4
|
1月前
|
SQL Java 数据库连接
从理论到实践:Hibernate与JPA在Java项目中的实际应用
本文介绍了Java持久层框架Hibernate和JPA的基本概念及其在具体项目中的应用。通过一个在线书店系统的实例,展示了如何使用@Entity注解定义实体类、通过Spring Data JPA定义仓库接口、在服务层调用方法进行数据库操作,以及使用JPQL编写自定义查询和管理事务。这些技术不仅简化了数据库操作,还显著提升了开发效率。
42 3
|
1月前
|
前端开发 Java 数据库
如何实现一个项目,小白做项目-java
本教程涵盖了从数据库到AJAX的多个知识点,并详细介绍了项目实现过程,包括静态页面分析、数据库创建、项目结构搭建、JSP转换及各层代码编写。最后,通过通用分页和优化Servlet来提升代码质量。
48 1
|
弹性计算 数据可视化 关系型数据库
阿里云服务器部署Java Web项目和连接MySQL数据库全流程
阿里云服务器部署Java Web项目和连接MySQL数据库全流程
6020 0
阿里云服务器部署Java Web项目和连接MySQL数据库全流程
|
4月前
|
Java 应用服务中间件 Windows
【应用服务 App Service】App Service 中部署Java项目,查看Tomcat配置及上传自定义版本
【应用服务 App Service】App Service 中部署Java项目,查看Tomcat配置及上传自定义版本