点餐系统的制作

2021/11/18 6:13:00

本文主要是介绍点餐系统的制作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

点餐系统运用技术有html,css,jquery,javascrip,servlet,jsp,xml,filter,jdbc

本系统数据库采用druid数据库连接与数据库进行连接,通过servet调用service,service调用到方法,实现系统后台操作,实现用户注册,登陆,添加订单,后台管理

用户进入该系统进入如下界面

在未登陆该系统情况下,点击后台管理filter会进行拦截,并跳入登陆界面

 

点击登录则会跳入登陆成功界面,并且通过el标签将用户名打印到登陆成功界面,点击返回首页会调回一开头界面,且注册,登陆改为我的,订单

 

 

点击点餐入口会进入点餐界面,改界面通过反问servlet,将数据库数据打印到界面,点击页面下的页脚可以进行跳转

点击末页跳转进入末页,点击加入我的订单,会将数据保存进入我的·数据库中,并且通过el表达式可以打印添加的商品

 

 

 点击我的订单则跳入我的订单界面,我的订单界面通过orderServlet,将上面添加的食谱从数据库中打印到此界面,并且计算出总数量和总金额,通过el表达式打印到界面

 主页中点击后台管理会进入如下界面,改界面可以进行数据修改,添加,删除,数据库会发生相应改变

 数据修改界面如下,点击那个修改,会把相应数据回显到界面

 druid数据库连接池连接数据库

package dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import until.JdbcUntil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BaseDao {
    private QueryRunner queryRunner=new QueryRunner();

    //增删改语句
    public int update(String sql,Object...args){
        Connection connection= JdbcUntil.getconnection();
        try {
            return queryRunner.update(connection,sql,args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUntil.close(connection);
        }
        return -1;
    }

    //查询一条语句
    public <T> T queryForOne(Class<T> type,String sql,Object...args){
        Connection getconnection = JdbcUntil.getconnection();
        try {
            return queryRunner.query(getconnection,sql,new BeanHandler<>(type),args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUntil.close(getconnection);
        }
        return null;
    }

    //查询多条语句
    public <T> List<T> queryForMany(Class<T> type,String sql,Object...args){
        Connection getconnection = JdbcUntil.getconnection();
        try {
            return queryRunner.query(getconnection,sql,new BeanListHandler<>(type),args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUntil.close(getconnection);
        }
        return null;
    }

    //执行返回一行一列的值
    public Object queryForSingle(String sql,Object ...args){
        Connection getconnection = JdbcUntil.getconnection();
        try {
            return queryRunner.query(getconnection,sql,new ScalarHandler(),args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUntil.close(getconnection);
        }
        return null;
    }

}

baseDao对数据库进行操作 

package until;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUntil {

    private static DruidDataSource druidDataSource;

    //数据库连接池的连接
    static {
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = JdbcUntil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(resourceAsStream);
            druidDataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //数据库连接池关闭
    public static Connection getconnection(){
        Connection connection = null;
        try {
            connection = druidDataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    public static void close(Connection connection){
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

}

我的订单servlet

package servlet;

import pojo.Cart;
import pojo.Food;
import pojo.Page;
import service.CartService;
import service.CartServiceimpl;
import service.FoodService;
import service.FoodServiceimpl;
import until.WebUntil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class CartServlet extends BaseServlet {
    private CartService cartService=new CartServiceimpl();
    private FoodService foodService=new FoodServiceimpl();

    //添加商品加入我的订单,如果我的订单存在,则修改数量和总价
    protected void addCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取食物id
        int id=WebUntil.parseInt(req.getParameter("id"),1);
        //查找该食物信息
        Food food=foodService.queryFoodById(id);
        int number=1;
        int totalPrice;
        //判断该食物是否存在
        if(cartService.existCart(food.getName())){
            totalPrice=number*food.getPrice();
            Cart cart=new Cart(null,food.getName(),food.getPrice(),number,totalPrice);
            cartService.addCart(cart);
        }else{
            //查找我的订单中该食物数量
            number=cartService.queryIdByName(food.getName()).getNumber();
            //数量增加
            number++;
            //重新计算总价格
            totalPrice=number*food.getPrice();
            //查找该食物id
            int orderid = cartService.queryIdByName(food.getName()).getOrderid();
            Cart cart=new Cart(orderid,food.getName(),food.getPrice(),number,totalPrice);
            cartService.updateCart(cart);
        }
        food.setStock(food.getStock()-1);
        cartService.updateFood(food);
        req.setAttribute("cart",cartService.queryIdByName(food.getName()));
        int pageNow=WebUntil.parseInt(req.getParameter("pageNow"),1);
        req.getRequestDispatcher("pageServlet?action=pageChinaFood&pageNow="+pageNow).forward(req,resp);
    }


    //删除我的订单
    protected void deleteCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=WebUntil.parseInt(req.getParameter("id"),0) ;
        cartService.deleteCart(id);
        int pageNow=WebUntil.parseInt(req.getParameter("pageNow"),1);
        req.getRequestDispatcher("cartServlet?action=readCart&pageNow="+pageNow).forward(req,resp);
    }

    //读取数据库数据到我的订单界面
    protected void readCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNow= WebUntil.parseInt(req.getParameter("pageNow"),1);
        int pageNumber=WebUntil.parseInt(req.getParameter("pageNumber"), Page.number);
        Page<Cart> cartPage=cartService.cartPage(pageNow,pageNumber);
        cartPage.setUrl("cartServlet?action=readCart");
        req.setAttribute("page",cartPage);
        List<Cart> cart=cartService.queryCart();
        int totalNumber=0;
        int totalPrices=0;
        for (Cart cart1:cart) {
            totalNumber=totalNumber+cart1.getNumber();
            totalPrices=totalPrices+cart1.getTotalPrice();
        }
        req.setAttribute("totalNumber",totalNumber);
        req.setAttribute("totalPrices",totalPrices);
        req.getRequestDispatcher("/user/order.jsp").forward(req,resp);
    }

}

后台管理servlet

package servlet;

import pojo.Food;
import pojo.Page;
import service.FoodService;
import service.FoodServiceimpl;
import service.UserService;
import service.UserServiceimpl;
import until.WebUntil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class FoodServlet extends BaseServlet{
    private FoodService foodService=new FoodServiceimpl();

    //添加菜系
    protected void addFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        int price=WebUntil.parseInt(req.getParameter("price"),0);
        int number = WebUntil.parseInt(req.getParameter("number"),0);
        int stock =WebUntil.parseInt(req.getParameter("stock"),0) ;
        Food food=new Food(null,name,price,number,stock,null);
        foodService.addFood(food);
        req.getRequestDispatcher("foodServlet?action=page&pageNow="+req.getParameter("pageNow")).forward(req,resp);

    }

    protected void getFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=WebUntil.parseInt(req.getParameter("id"),0);

        Food foods = foodService.queryFoodById(id);

        req.setAttribute("foods",foods);

        req.getRequestDispatcher("user/updateFood.jsp").forward(req,resp);
    }

    //修改菜系
    protected void updateFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Food food = WebUntil.copy(req.getParameterMap(), new Food());
        System.out.println(food);
        foodService.updateFood(food);
        req.getRequestDispatcher("foodServlet?action=page&pageNow="+req.getParameter("pageNow")).forward(req,resp);
    }

    //删除菜系
    protected void deleteFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=WebUntil.parseInt(req.getParameter("id"),0);
        foodService.deleteFood(id);
        req.getRequestDispatcher("foodServlet?action=page&pageNow="+req.getParameter("pageNow")).forward(req,resp);
    }

    //读取菜系到页面
    protected void readFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Food> food=foodService.readFood();
        //保存菜系数据域
        req.setAttribute("food",food);
        //页面跳转回后台界面
        req.getRequestDispatcher("user/manager.jsp").forward(req,resp);
    }

    //后台数据制作分页
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前页码
        int pageNow= WebUntil.parseInt(req.getParameter("pageNow"),1);
        //获取一个界面出现个数
        int pageNumber=WebUntil.parseInt(req.getParameter("pageNumber"), Page.number);
        //从数据库查找当前数据
        Page<Food> foods= foodService.page(pageNow,pageNumber);
        foods.setUrl("foodServlet?action=page");

        req.setAttribute("page",foods);
        req.getRequestDispatcher("/user/manager.jsp").forward(req,resp);


    }
}

 分页servlet

package servlet;

import pojo.Food;
import pojo.Page;
import service.FoodService;
import service.FoodServiceimpl;
import until.WebUntil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class PageServlet extends BaseServlet{
    private FoodService foodService=new FoodServiceimpl();

    //处理主页数据以及分页功能
    protected void pageChinaFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前页码
        int pageNow= WebUntil.parseInt(req.getParameter("pageNow"),1);
        //获取一个界面出现个数
        int pageNumber=WebUntil.parseInt(req.getParameter("pageNumber"), Page.number);
        //从数据库查找当前数据
        Page<Food> foods= foodService.page(pageNow,pageNumber);
        foods.setUrl("pageServlet?action=pageChinaFood");

        req.setAttribute("page",foods);
        req.getRequestDispatcher("/user/mainChinaFood.jsp").forward(req,resp);

    }
}

 用户功能sevlet

package servlet;

import pojo.User;
import service.UserService;
import service.UserServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends BaseServlet{
    protected UserService userService=new UserServiceimpl();
//注册
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");

        User user = new User(null, username, password, phone);
        if(userService.exitUsername(username)){
            userService.register(user);
            req.getRequestDispatcher("/user/register_successs.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("/user/regist.jsp").forward(req,resp);
        }
    }
//登陆
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User(null, username, password, null);
        if(userService.login(user)==null){
            req.getRequestDispatcher("/user/login.jsp").forward(req,resp);
        }else{
            req.getSession().setAttribute("user",user);
            req.getRequestDispatcher("/user/login_success.jsp").forward(req,resp);

        }
    }

 //退出登录
    protected void loginout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
    }
}

 前端界面,

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base href="http://localhost:8080/OrderFood/">
    <link type="text/css" rel="stylesheet" href="static/css/mainChinaFood.css">
    <link rel="stylesheet" type="text/css" href="static/style(js)/bootstrap.bundle.min.js">
    <link rel="stylesheet" type="text/css" href="static/style(css)/bootstrap.min.css">
    <script type="text/javascript" src="static/lib/jquery-1.7.2.js"></script>
    <script type="text/javascript" >
        $(function () {
            $("#button").click(function (){
                location.href="user/order.jsp";
            })
        })
    </script>
</head>
<body>
    <div id="div1">
        <div id="font">食谱大全</div>
        <a href="cartServlet?action=readCart" type="button" class="btn btn-link" id="button">我的订单</a>
<!--        商品列表-->
        <div id="background">
            <c:forEach items="${requestScope.page.foods}" var="food">
                <div id="box">
                    <img src="static/piture/小笼包.jpg" alt="" id="img1">
                    <div id="d1">菜品名称:${food.name}</div>
                    <div id="d2">菜品价格:${food.price}元/份</div>
                    <div id="d3">菜品数量:${food.stock}份</div>
                    <a href="cartServlet?action=addCart&id=${food.id}&pageNow=${requestScope.page.pageNow}" id="button1" type="button" class="btn btn-dark btn-sm">加入我的订单</a>
                </div>
            </c:forEach>



<!--            分页区域-->
          <%@include file="/common/page.jsp"%>
          <script type="text/javascript">
              $(function () {
                  $("#pages").click(function () {
                        var a=$("#inputs").val()

                        location.href="${pageScope.basePath}${requestScope.page.url}&pageNow="+a
                  })
              })
          </script>

            <div style="font-size: 1.3pc;color: blue;position:absolute;top: 83%;left: 38%">您刚刚添加了${requestScope.cart.ordername}进入我的订单</div>

        </div>

    </div>
</body>
</html>


这篇关于点餐系统的制作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程