jsp + servlet online ordering system (front desk, backend) based on javaweb + mysql (java + jsp + servlet + mysql + dbutil + tomcat)
Private message Source code acquisition and debugging communication
Operating environment
Java≥8, MySQL≥5.7, Tomcat≥8
development tools
eclipse/idea/myeclipse/sts, etc. can be configured to run
Be applicable
Course design, major assignments, graduation projects, project exercises, learning demonstrations, etc.
Function Description
Backstage: login, category management, dish management, member management, order management, message management, etc.
Front desk: log in, register, view dishes, purchase, leave messages
front desk
Backstage
technical framework
JSP Servlet MySQL DBUtil Tomcat JavaScript CSS jstl
JSP + Servlet online ordering system (front desk, backend) based on javaweb + mysql (java + jsp + servlet + mysql + dbutil + tomcat)
\t public void xinyongMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String user_id=req.getParameter("user_id").trim(); \t\t List xinyongList=new ArrayList(); String sql="select * from t_xinyong where user_id=?"; Object[] params={<!-- -->user_id}; DB mydb=new DB(); try {<!-- --> mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) {<!-- --> Txinyong xinyong=new Txinyong(); \t\t\t\t xinyong.setId(rs.getString("id")); xinyong.setShuxing(rs.getString("shuxing")); xinyong.setNeirong(rs.getString("neirong")); xinyong.setShijian(rs.getString("shijian")); \t\t\t\t xinyong.setUser_id(rs.getString("user_id")); \t\t\t\t xinyongList.add(xinyong); } rs.close(); } catch(Exception e) {<!-- --> e.printStackTrace(); } mydb.closed(); \t\t req.setAttribute("xinyongList", xinyongList); req.getRequestDispatcher("admin/xinyong/xinyongMana.jsp").forward(req, res); } \t \t \t public void xinyongDel(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String id=req.getParameter("id").trim(); \t\t String sql="delete from t_xinyong where id=?"; Object[] params={<!-- -->id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "Operation successful"); String targetURL = "/common/msg.jsp";
//Add items to shopping cart {<!-- --> String goods_id=req.getParameter("goods_id");//Goods ID int quantity=Integer.parseInt(req.getParameter("quantity"));//Quantity of goods Tgoods goods=liuService.getGoods(goods_id);//Goods ID, given to good \t\t TorderItem orderItem=new TorderItem(); orderItem.setGoods(goods);//Set the product ID orderItem.setGoods_quantity(quantity);//Set the quantity of goods \t\t HttpSession session=req.getSession(); Cart cart =(Cart)session.getAttribute("cart"); cart.addGoods(goods_id, orderItem); \t\t session.setAttribute("cart", cart); \t\t req.setAttribute("message", "Operation successful"); req.setAttribute("path", "site/cart/mycart.jsp");//Add shopping cart and jump to this page successfully \t\t String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res);//Jump to targetURL \t\t } \t \t \t public void orderSubmit(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException \t//Submit Order {<!-- --> String songhuodizhi=req.getParameter("songhuodizhi");//Get the shipping address String fukuanfangshi=req.getParameter("fukuanfangshi");//Get the payment method \t\t HttpSession session=req.getSession(); Cart cart =(Cart)session.getAttribute("cart"); Tuser user=(Tuser)session.getAttribute("user"); \t\t Torder order=new Torder(); \t\t order.setId(String.valueOf(new Date().getTime()));//Set the order ID in time format order.setBianhao(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));//Set the order number in the format of yyyyMMddhhmmss order.setShijian(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));//Set the order time, the format is yyyy-MM-dd hh:mm: ss order.setZhuangtai("no"); \t\t order.setHuifu(""); \t\t order.setSonghuodizhi(songhuodizhi); order.setFukuanfangshi(fukuanfangshi); order.setJine(cart.getTotalPrice());//Set the total amount order.setUser_id(user.getId()); \t\t liuService.saveOrder(order); \t\t
String id=req.getParameter("id"); \t\t List userList=new ArrayList(); String sql="select * from t_user where id=?"; Object[] params={<!-- -->id}; DB mydb=new DB(); try {<!-- --> mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) {<!-- --> Tuser user=new Tuser(); \t\t\t\t user.setId(rs.getString("id")); user.setLoginname(rs.getString("loginname")); user.setLoginpw(rs.getString("loginpw")); user.setLoginpw(rs.getString("loginpw")); user.setName(rs.getString("name")); \t\t\t\t userList.add(user); } rs.close(); } catch(Exception e) {<!-- --> e.printStackTrace(); } mydb.closed(); \t\t req.setAttribute("userList", userList); req.setAttribute("xinyongList", liuService.getxinyongList(id)); req.getRequestDispatcher("admin/user/userDetail.jsp").forward(req, res); } \t \t public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) {<!-- --> RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try {<!-- --> dispatch.forward(request, response); return; } catch (ServletException e) {<!-- --> e.printStackTrace();
{<!-- --> xinyongMana(req, res);//Credit management } if(type.endsWith("xinyongDel")) {<!-- --> xinyongDel(req, res);//Credit deletion } } \t public void xinyongAdd(HttpServletRequest req,HttpServletResponse res) {<!-- --> String id=String.valueOf(new Date().getTime()); String shuxing=req.getParameter("shuxing").trim(); String neirong=req.getParameter("neirong").trim(); String shijian=req.getParameter("shijian").trim(); \t\t String user_id=req.getParameter("user_id").trim(); \t\t String sql="insert into t_xinyong(id,shuxing,neirong,shijian,user_id) values(?,?,?,?,?)"; Object[] params={<!-- -->id,shuxing,neirong,shijian,user_id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "Operation successful"); String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t \t public void xinyongMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String user_id=req.getParameter("user_id").trim(); \t\t List xinyongList=new ArrayList(); String sql="select * from t_xinyong where user_id=?"; Object[] params={<!-- -->user_id}; DB mydb=new DB(); try {<!-- --> mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) {<!-- --> Txinyong xinyong=new Txinyong(); \t\t\t\t xinyong.setId(rs.getString("id")); xinyong.setShuxing(rs.getString("shuxing")); xinyong.setNeirong(rs.getString("neirong")); xinyong.setShijian(rs.getString("shijian"));
String neirong=req.getParameter("neirong").trim(); String shijian=req.getParameter("shijian").trim(); \t\t String user_id=req.getParameter("user_id").trim(); \t\t String sql="insert into t_xinyong(id,shuxing,neirong,shijian,user_id) values(?,?,?,?,?)"; Object[] params={<!-- -->id,shuxing,neirong,shijian,user_id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "Operation successful"); String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t \t public void xinyongMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String user_id=req.getParameter("user_id").trim(); \t\t List xinyongList=new ArrayList(); String sql="select * from t_xinyong where user_id=?"; Object[] params={<!-- -->user_id}; DB mydb=new DB(); try {<!-- --> mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) {<!-- --> Txinyong xinyong=new Txinyong(); \t\t\t\t xinyong.setId(rs.getString("id")); xinyong.setShuxing(rs.getString("shuxing")); xinyong.setNeirong(rs.getString("neirong")); xinyong.setShijian(rs.getString("shijian")); \t\t\t\t xinyong.setUser_id(rs.getString("user_id")); \t\t\t\t xinyongList.add(xinyong);
String name=req.getParameter("name"); String del="no"; \t\t String s=liuService.panduan_zhanghao(loginname); if(s.equals("yizhan")) {<!-- --> req.setAttribute("message", "The account has been occupied, please enter another account"); req.setAttribute("path", "site/userreg/userreg.jsp"); \t\t\t String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } else {<!-- --> String sql="insert into t_user values(?,?,?,?,?)"; Object[] params={<!-- -->id,loginname,loginpw,name,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t\t req.setAttribute("message", "Registration successful, please log in"); req.setAttribute("path", "site/default.jsp"); \t\t\t String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } \t\t \t\t } \t \t \t public void userLogout(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> HttpSession session=req.getSession(); session.setAttribute("userType", null); session.setAttribute("user", null); \t\t \t\t\t req.setAttribute("message", "Successfully exited the system"); req.setAttribute("path", "site/default.jsp"); String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } \t \t public void userMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> List userList=new ArrayList();
public class buy_servlet extends HttpServlet {<!-- --> \t public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException //request object req, response object res {<!-- --> String type=req.getParameter("type");//Get the type attribute and give it to type \t\t if(type.endsWith("addToCart"))//If the parameter passed in the URL is addToCart {<!-- --> addToCart(req, res);//Execute adding shopping cart } if(type.endsWith("orderSubmit"))//If the parameter passed in the URL is orderSubmit {<!-- --> orderSubmit(req, res);//Execute the submit order function } if(type.endsWith("myorder")) {<!-- --> myorder(req, res);//Execute my order function } if(type.endsWith("orderDetail")) {<!-- --> orderDetail(req, res);//Execute order detail function } \t\t \t\t \t\t } \t \t public void addToCart(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException //Add items to shopping cart {<!-- --> String goods_id=req.getParameter("goods_id");//Goods ID int quantity=Integer.parseInt(req.getParameter("quantity"));//Quantity of goods Tgoods goods=liuService.getGoods(goods_id);//Goods ID, given to good \t\t TorderItem orderItem=new TorderItem(); orderItem.setGoods(goods);//Set the product ID orderItem.setGoods_quantity(quantity);//Set the quantity of goods \t\t HttpSession session=req.getSession();
String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t \t public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) //Jump to next page {<!-- --> RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try {<!-- --> dispatch.forward(request, response); return; } catch (ServletException e) {<!-- --> e.printStackTrace(); } catch (IOException e) {<!-- --> \t\t\t e.printStackTrace(); } } public void init(ServletConfig config) throws ServletException //Initialize configuration {<!-- --> super.init(config); } \t public void destroy() {<!-- --> \t\t } } public class cartService {<!-- --> public String modiNum(String goodsId,int quantity)//Define a public class to change the quantity of goods in the shopping cart {<!-- -->
int goods_quantity=orderItem.getGoods_quantity(); liuService.saveOrderItem(id, order_id, goods_id, goods_quantity);//Save id, order_id, goods_id, goods_quantity \t\t\t } \t\t cart.getItems().clear(); //Clear the shopping cart session.setAttribute("cart", cart); \t\t req.setAttribute("order", order); req.getRequestDispatcher("site/order/orderSubmit.jsp").forward(req, res);//Jump to this interface } \t \t public void myorder(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException \t//My Order {<!-- --> HttpSession session=req.getSession(); Tuser user=(Tuser)session.getAttribute("user"); \t\t req.setAttribute("orderList", liuService.orderList(user.getId()));//Set the attributes of orderList req.getRequestDispatcher("site/order/myorder.jsp").forward(req, res); } \t public void orderDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException \t//Order Details {<!-- --> String order_id=req.getParameter("order_id"); System.out.println(order_id + "DD");//Output order_idDD req.setAttribute("orderItemList", liuService.orderItemList(order_id)); req.getRequestDispatcher("site/order/orderDetail.jsp").forward(req, res); } \t \t public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) //Jump to the next interface {<!-- --> RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try {<!-- --> dispatch.forward(request, response); return; } catch (ServletException e) {<!-- --> e.printStackTrace(); } catch (IOException e) {<!-- --> \t\t\t e.printStackTrace();
req.setAttribute("goodsList", goodsList); req.getRequestDispatcher("site/goods/goodsNew.jsp").forward(req, res); } \t \t \t \t public void goodsByCatelog(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException //Dish category {<!-- --> String catelog_id=req.getParameter("catelog_id"); \t\t req.setAttribute("goodsList", liuService.goodsByCatelog(catelog_id)); req.getRequestDispatcher("site/goods/goodsByCatelog.jsp").forward(req, res); } \t public void goodsDetailQian(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException //Details of the dish front desk {<!-- --> String id=req.getParameter("id"); \t\t req.setAttribute("goods", liuService.getGoods(id)); req.getRequestDispatcher("site/goods/goodsDetailQian.jsp").forward(req, res); } \t \t public void goodsRes(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException //Information before the dish {<!-- --> String mingcheng=req.getParameter("mingcheng"); \t\t List goodsList=new ArrayList(); String sql="select * from t_goods where del='no' and mingcheng like '%" + mingcheng.trim() + "%'"; Object[] params={<!-- -->}; DB mydb=new DB(); try {<!-- --> mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) {<!-- --> Tgoods goods=new Tgoods(); \t\t\t\t goods.setId(rs.getString("id")); goods.setCatelog_id(rs.getString("catelog_id")); goods.setBianhao(rs.getString("bianhao")); \t\t\t\t goods.setMingcheng(rs.getString("mingcheng")); goods.setJieshao(rs.getString("jieshao")); goods.setFujian(rs.getString("fujian")); \t\t\t\t
Tuser user=new Tuser(); \t\t\t\t\t user.setId(rs.getString("id")); user.setLoginname(rs.getString("loginname")); user.setLoginpw(rs.getString("loginpw")); user.setName(rs.getString("name")); \t\t\t\t\t user.setDel(rs.getString("del")); \t\t\t\t\t WebContext ctx = WebContextFactory.get(); HttpSession session=ctx.getSession(); \t\t\t\t\t session.setAttribute("userType", 1); session.setAttribute("user", user); Cart cart=new Cart(); session.setAttribute("cart", cart); } rs.close(); } catch(Exception e) {<!-- --> e.printStackTrace(); } mydb.closed(); } if(userType==2) {<!-- --> \t\t\t } return result; } \t \t public String adminPwEdit(String userPwNew) //Administrator password editing {<!-- --> System.out.println("DDDD"); try {<!-- --> Thread.sleep(700);//Ensure thread running order } catch (InterruptedException e) {<!-- --> e.printStackTrace(); } WebContext ctx = WebContextFactory.get(); HttpSession session=ctx.getSession();
mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "Information deletion completed"); String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t \t \t public void orderShouli(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String id=req.getParameter("id"); \t\t String sql="update t_order set zhuangtai='yes' where id=?"; Object[] params={<!-- -->id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "Order accepted"); String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t \t public void huifuAdd(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String id=req.getParameter("id"); String huifu=req.getParameter("huifu"); \t\t String sql="update t_order set huifu=? where id=?"; Object[] params={<!-- -->huifu,id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "Operation successful"); String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) {<!-- --> RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try {<!-- --> dispatch.forward(request, response); return; } catch (ServletException e) {<!-- --> e.printStackTrace(); }
req.setAttribute("message", "The account has been occupied, please enter another account"); req.setAttribute("path", "site/userreg/userreg.jsp"); \t\t\t String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } else {<!-- --> String sql="insert into t_user values(?,?,?,?,?)"; Object[] params={<!-- -->id,loginname,loginpw,name,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t\t req.setAttribute("message", "Registration successful, please log in"); req.setAttribute("path", "site/default.jsp"); \t\t\t String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } \t\t \t\t } \t \t \t public void userLogout(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> HttpSession session=req.getSession(); session.setAttribute("userType", null); session.setAttribute("user", null); \t\t \t\t\t req.setAttribute("message", "Successfully exited the system"); req.setAttribute("path", "site/default.jsp"); String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } \t \t public void userMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> List userList=new ArrayList(); String sql="select * from t_user where del='no'"; Object[] params={<!-- -->}; DB mydb=new DB(); try {<!-- -->
public class loginService {<!-- --> public String login(String userName,String userPw,int userType) {<!-- --> String result="no"; \t\t if(userType==0)//System administrator login {<!-- --> String sql="select * from t_admin where userName=? and userPw=?"; Object[] params={<!-- -->userName,userPw}; DB mydb=new DB(); mydb.doPstm(sql, params); try {<!-- --> ResultSet rs=mydb.getRs(); boolean mark=(rs==null||!rs.next()?false:true);//If the result set is empty, return false, if the result set is not empty, return true if(mark==false) {<!-- --> result="no"; } else {<!-- --> result="yes"; TAdmin admin=new TAdmin(); admin.setUserId(rs.getInt("userId")); admin.setUserName(rs.getString("userName")); admin.setUserPw(rs.getString("userPw")); WebContext ctx = WebContextFactory.get(); //Get the WebContext object ctx HttpSession session=ctx.getSession(); session.setAttribute("userType", 0); session.setAttribute("admin", admin); } rs.close(); } catch (SQLException e) {<!-- --> System.out.println("Login failed!"); e.printStackTrace(); } finally {<!-- --> mydb.closed(); } \t\t\t } \t\t \t\t if(userType==1)//Member login {<!-- -->
try {<!-- --> mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) {<!-- --> Tuser user=new Tuser(); \t\t\t\t user.setId(rs.getString("id")); user.setLoginname(rs.getString("loginname")); user.setLoginpw(rs.getString("loginpw")); user.setLoginpw(rs.getString("loginpw")); user.setName(rs.getString("name")); \t\t\t\t userList.add(user); } rs.close(); } catch(Exception e) {<!-- --> e.printStackTrace(); } mydb.closed(); \t\t req.setAttribute("userList", userList); req.getRequestDispatcher("admin/user/userMana.jsp").forward(req, res); } \t \t \t public void userDel(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String id=req.getParameter("id"); \t\t String sql="update t_user set del='yes' where id=?"; Object[] params={<!-- -->id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); \t\t req.setAttribute("msg", "User information deleted"); String targetURL = "/common/msg.jsp"; dispatch(targetURL, req, res); } \t \t public void userDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {<!-- --> String id=req.getParameter("id");
} } public class xinyong_servlet extends HttpServlet {<!-- --> public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException {<!-- --> String type=req.getParameter("type"); \t\t if(type.endsWith("xinyongAdd")) {<!-- --> xinyongAdd(req, res);//Credit addition } if(type.endsWith("xinyongMana")) {<!-- --> xinyongMana(req, res);//Credit management } if(type.endsWith("xinyongDel")) {<!-- --> xinyongDel(req, res);//Credit deletion } }
//Create the iterator object it, the value is the value of the product quantity, if there is something after the iterator, execute the loop {<!-- --> TorderItem orderItem = (TorderItem) it.next();//Get the next object, convert it into a TorderItem object, and give the TorderItem object orderItem \t\t\t String id=String.valueOf(new Date().getTime());//Get the number ID String order_id=order.getId();//Get the order ID String goods_id=orderItem.getGoods().getId(); int goods_quantity=orderItem.getGoods_quantity(); liuService.saveOrderItem(id, order_id, goods_id, goods_quantity);//Save id, order_id, goods_id, goods_quantity \t\t\t } \t\t cart.getItems().clear(); //Clear the shopping cart session.setAttribute("cart", cart); \t\t req.setAttribute("order", order); req.getRequestDispatcher("site/order/orderSubmit.jsp").forward(req, res);//Jump to this interface } \t \t public void myorder(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException \t//My Order {<!-- --> HttpSession session=req.getSession(); Tuser user=(Tuser)session.getAttribute("user"); \t\t req.setAttribute("orderList", liuService.orderList(user.getId()));//Set the attributes of orderList req.getRequestDispatcher("site/order/myorder.jsp").forward(req, res); } \t public void orderDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException \t//Order Details {<!-- --> String order_id=req.getParameter("order_id"); System.out.println(order_id + "DD");//Output order_idDD req.setAttribute("orderItemList", liuService.orderItemList(order_id)); req.getRequestDispatcher("site/order/orderDetail.jsp").forward(req, res); } \t \t public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) //Jump to the next interface {<!-- --> RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try