您好,登錄后才能下訂單哦!
這篇文章主要介紹Java如何實現共享租車信息管理系統,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
基于servlet+jsp+jdbc的后臺管理系統,包含5個模塊:汽車賬戶部管理、租車賬戶部管理、汽車信息管理表、租車記錄表、租車租聘表。功能完整,均能實現增刪查改。
運行環境: jdk8+tomcat8.5+mysql5.7+Eclipse
項目技術: servlet+jsp+jdbc+easyui+jquery
主頁登陸注冊業務:
public class ForeServlet extends BaseForeServlet { /** * 顯示主頁 * * @param request * @param response * @param page * @return */ public String home(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 ForeServlet home方法"); List<Category> cs = new CategoryDAO().list(); new ProductDAO().fill(cs); new ProductDAO().fillByRow(cs); request.setAttribute("cs", cs); return "home.jsp"; } /** * 注冊 * * @param request * @param response * @param page * @return */ public String register(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 ForeServlet register方法"); Map<String, String> params = new HashMap<>(); InputStream inputStream = super.parseUpload(request, params); System.out.println(params); String name = params.get("name"); String password = params.get("password"); System.out.println(name); // 判斷用戶名是否存在 boolean exist = userDAO.isExists(name); if (exist) { request.setAttribute("msg", "用戶名已經被使用"); return "register.jsp"; } User user = new User(); user.setName(name); user.setPassword(password); System.out.println(user.getName()); System.out.println(user.getPassword()); userDAO.add(user); return "@registerSuccess.jsp"; } /** * 登錄 * * @param request * @param response * @param page * @return */ public String login(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 ForeServlet login方法"); Map<String, String> params = new HashMap<>(); InputStream inputStream = super.parseUpload(request, params); System.out.println(params); String name = params.get("name"); String password = params.get("password"); // 登錄 User user = userDAO.get(name, password); if (null == user) { request.setAttribute("msg", "賬號密碼錯誤"); return "login.jsp"; } // 將賬號密碼儲存在Session中 request.getSession().setAttribute("user", user); return "@forehome"; } /** * 登出 * * @param request * @param response * @param page * @return */ public String logout(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 ForeServlet logout方法"); request.getSession().removeAttribute("user"); return "@forehome"; } /** * 檢查用戶是否登錄 * * @param request * @param response * @param page * @return */ public String checkLogin(HttpServletRequest request, HttpServletResponse response, Page page) { User user = (User) request.getSession().getAttribute("user"); if (user != null) { return "%success"; } return "%fail"; } /** * 模態登錄框通過 Ajax 方法登錄 * * @param request * @param response * @param page * @return */ public String loginAjax(HttpServletRequest request, HttpServletResponse response, Page page) { String name = request.getParameter("name"); String password = request.getParameter("password"); User user = userDAO.get(name, password); if (user == null) return "%fail"; request.getSession().setAttribute("user", user); return "%success"; } /** * 顯示產品頁 * * @param request * @param response * @param page * @return */ public String product(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了 ForeServlet product方法"); int pid = Integer.parseInt(request.getParameter("pid")); Product p = productDAO.get(pid); // 獲取產品的single圖片和detail圖片 List<ProductImage> productSingleImages = productImageDAO.list(p, ProductImageDAO.type_single); List<ProductImage> productDetailImages = productImageDAO.list(p, ProductImageDAO.type_detail); p.setProductSingleImages(productSingleImages); p.setProductDetailImages(productDetailImages); //p.setFirstProductImage(productSingleImages.get(0)); // 獲取產品屬性信息 List<PropertyValue> pvs = propertyValueDAO.list(p.getId()); // 獲取產品評價信息 List<Review> reviews = reviewDAO.list(p.getId()); // 設置產品銷量數和評價數 productDAO.setSaleAndReviewNumber(p); request.setAttribute("p", p); request.setAttribute("pvs", pvs); request.setAttribute("reviews", reviews); return "product.jsp"; } /** * 分類頁面 * * @return */ public String category(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet category方法"); int cid = Integer.parseInt(request.getParameter("cid")); // 獲取分類對象并為填充產品對象、銷量、評價 Category c = new CategoryDAO().get(cid); new ProductDAO().fill(c); new ProductDAO().setSaleAndReviewNumber(c.getProducts()); // 根據獲取的排序方式進行排序 String sort = request.getParameter("sort"); if (null != sort) { switch (sort) { case "review": Collections.sort(c.getProducts(), new ProductReviewComparator()); break; case "date": Collections.sort(c.getProducts(), new ProductDateComparator()); break; case "saleCount": Collections.sort(c.getProducts(), new ProductSaleCountComparator()); break; case "price": Collections.sort(c.getProducts(), new ProductPriceComparator()); break; case "all": Collections.sort(c.getProducts(), new ProductAllComparator()); break; } } // 排序完成后,將分類對象添加到域對象 request 中 request.setAttribute("c", c); return "category.jsp"; } /** * 搜索功能 * * @param request * @param response * @param page * @return */ public String search(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet search方法"); String keyword = request.getParameter("keyword"); List<Product> ps = null; // 加入異常,防止崩潰 try { ps = productDAO.search(keyword, 0, 50); } catch (Exception e) { e.printStackTrace(); } // 設置產品的銷量和評論 productDAO.setSaleAndReviewNumber(ps); request.setAttribute("ps", ps); return "searchResult.jsp"; } /** * 立即購買 * * @param request * @param response * @param page * @return */ public String buyone(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet buyone方法"); int pid = Integer.parseInt(request.getParameter("pid")); int num = Integer.parseInt(request.getParameter("num")); Product p = productDAO.get(pid); User user = (User) request.getSession().getAttribute("user"); List<OrderItem> ois = orderItemDAO.listByUser(user.getId()); int oiid = 0; boolean found = false; // 遍歷訂單,如果訂單已存在對應的產品,就在對應購物車的基礎上調整數量 if (null != ois) { for (OrderItem oi : ois) { if (oi.getProduct().getId() == p.getId()) { oi.setNumber(oi.getNumber() + num); orderItemDAO.update(oi); found = true; oiid = oi.getId(); break; } } } // 如果沒找到對應的商品,則新建一條訂單項 if (!found) { OrderItem oi = new OrderItem(); oi.setUser(user); oi.setProduct(p); oi.setNumber(num); orderItemDAO.add(oi); oiid = oi.getId(); } return "@forebuy?oiid=" + oiid; } /** * 結算頁面 * * @param request * @param response * @param page * @return */ public String buy(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet buy方法"); String[] oiids = request.getParameterValues("oiid"); List<OrderItem> ois = new ArrayList<>(); float total = 0; for (String strid : oiids) { int oiid = Integer.parseInt(strid); OrderItem oi = orderItemDAO.get(oiid); total += oi.getProduct().getPromotePrice() * oi.getNumber(); ois.add(oi); } request.getSession().setAttribute("ois", ois); request.setAttribute("total", total); return "buy.jsp"; } /** * 購物車 * * @param request * @param response * @param page * @return */ public String cart(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet cart方法"); User user = (User) request.getSession().getAttribute("user"); List<OrderItem> ois = orderItemDAO.listByUser(user.getId()); request.setAttribute("ois", ois); return "cart.jsp"; } /** * 加入購物車 * * @param request * @param response * @param page * @return */ public String addCart(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet addCart方法"); int pid = Integer.parseInt(request.getParameter("pid")); int num = Integer.parseInt(request.getParameter("num")); Product p = productDAO.get(pid); User user = (User) request.getSession().getAttribute("user"); List<OrderItem> ois = orderItemDAO.listByUser(user.getId()); int oiid = 0; boolean found = false; // 遍歷訂單,如果訂單已存在對應的產品,就在對應購物車的基礎上調整數量 if (null != ois) { for (OrderItem oi : ois) { if (oi.getProduct().getId() == p.getId()) { oi.setNumber(oi.getNumber() + num); orderItemDAO.update(oi); found = true; oiid = oi.getId(); break; } } } // 如果沒找到對應的商品,則新建一條訂單項 if (!found) { OrderItem oi = new OrderItem(); oi.setUser(user); oi.setProduct(p); oi.setNumber(num); orderItemDAO.add(oi); oiid = oi.getId(); } return "%success"; } /** * 更改當前訂單項的內容 * * @param request * @param response * @param page * @return * @throws UnsupportedEncodingException */ public String changeOrderItem(HttpServletRequest request, HttpServletResponse response, Page page) throws UnsupportedEncodingException { System.out.println("調用了ForeServlet changeOrderItem方法"); User user = (User) request.getSession().getAttribute("user"); if (null == user) return "%fail"; int pid = Integer.parseInt(request.getParameter("pid")); int number = Integer.parseInt(request.getParameter("number")); List<OrderItem> ois = orderItemDAO.listByUser(user.getId()); for (OrderItem oi : ois) { if (oi.getProduct().getId() == pid) { oi.setNumber(number); orderItemDAO.update(oi); break; } } return "%success"; } /** * 刪除訂單項 * * @param request * @param response * @param page * @return * @throws UnsupportedEncodingException */ public String deleteOrderItem(HttpServletRequest request, HttpServletResponse response, Page page) throws UnsupportedEncodingException { System.out.println("調用了ForeServlet deleteOrderItem方法"); User user = (User) request.getSession().getAttribute("user"); if (null == user) return "%fail"; int oiid = Integer.parseInt(request.getParameter("oiid")); orderItemDAO.delete(oiid); return "%success"; } /** * 創建訂單 * * @param request * @param response * @param page * @return * @throws UnsupportedEncodingException */ public String createOrder(HttpServletRequest request, HttpServletResponse response, Page page) throws UnsupportedEncodingException { System.out.println("調用了ForeServlet createOrder方法"); // 防止post方法中文亂碼 request.setCharacterEncoding("UTF-8"); User user = (User) request.getSession().getAttribute("user"); List<OrderItem> ois = (List<OrderItem>) request.getSession().getAttribute("ois"); if (ois.isEmpty()) return "@login.jsp"; String address = request.getParameter("address"); String post = request.getParameter("post"); String reciver = request.getParameter("reciver"); String mobile = request.getParameter("mobile"); Order order = new Order(); String orderCode = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); order.setOrderCode(orderCode); order.setAddress(address); order.setPost(post); order.setReceiver(reciver); order.setMobile(mobile); order.setUserMessage("默認內容"); order.setCreateDate(new Date()); order.setUser(user); order.setStatus(OrderDAO.waitPay); orderDAO.add(order); // 統計總金額并設置每個訂單項的所屬訂單 float total = 0; for (OrderItem oi : ois) { oi.setOrder(order); orderItemDAO.update(oi); total += oi.getProduct().getPromotePrice() * oi.getNumber(); } return "@forealipay?oid=" + order.getId() + "&total=" + total; } /** * 支付界面 * * @param request * @param response * @param page * @return */ public String alipay(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet alipay方法"); return "alipay.jsp"; } /** * 支付完成界面 * @param request * @param response * @param page * @return */ public String payed(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet payed方法"); int oid = Integer.parseInt(request.getParameter("oid")); Order order = orderDAO.get(oid); order.setStatus(OrderDAO.waitDelivery); order.setPayDate(new Date()); orderDAO.update(order); request.setAttribute("o", order); return "payed.jsp"; } /** * 我的訂單頁面 * @param request * @param response * @param page * @return */ public String bought(HttpServletRequest request, HttpServletResponse response, Page page) { System.out.println("調用了ForeServlet bought方法"); User user = (User) request.getSession().getAttribute("user"); List<Order> os = orderDAO.list(user.getId(), OrderDAO.delete); orderItemDAO.fill(os); request.setAttribute("os", os); return "bought.jsp"; } }
用戶登陸頁面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <title>用戶登錄</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap.css" rel="external nofollow" > <link type="text/css" rel="stylesheet" href="<%=request.getContextPath()%>/css/style.css" rel="external nofollow" > <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/js/simpleCart.min.js"></script> </head> <body> <!--header--> <jsp:include page="header.jsp"> <jsp:param name="flag" value="9"></jsp:param> </jsp:include> <!--//header--> <!--account--> <div class="account"> <div class="container"> <div class="register"> <c:if test="${!empty msg }"> <div class="alert alert-success">${msg }</div> </c:if> <c:if test="${!empty failMsg }"> <div class="alert alert-danger">${failMsg }</div> </c:if> <form action="<%=request.getContextPath()%>/user_login" method="post"> <div class="register-top-grid"> <h4>用戶登錄</h4> <div class="input"> <span>用戶名/郵箱 <label >*</label></span> <input type="text" name="ue" placeholder="請輸入用戶名" required="required"> </div> <div class="input"> <span>密碼 <label >*</label></span> <input type="text" name="password" placeholder="請輸入密碼" required="required"> </div> <div class="clearfix"> </div> </div> <div class="register-but text-center"> <input type="submit" value="提交"> <div class="clearfix"> </div> </div> </form> <div class="clearfix"> </div> </div> </div> </div> <!--//account--> <!--footer--> <jsp:include page="footer.jsp"></jsp:include> <!--//footer--> </body> </html>
添加類型業務:
@WebServlet("/addCategoryServlet") public class AddCategoryServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String a = request.getParameter("parentId"); String b = request.getParameter("className"); System.out.println("parentId"+a); System.out.println("className"+b); Category category = new Category(); category.setCategory_parentid(Integer.valueOf(a)); category.setCategory_name(b); CategoryService service = new CategoryServiceImpl(); service.addCategory(category); PrintWriter out = response.getWriter(); out.write("<script>"); out.write("alert('插入分類成功!');"); out.write("location.href='/HOMEECMS/categoryListServlet'"); out.write("</script>"); out.close(); // request.getRequestDispatcher("").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
以上是“Java如何實現共享租車信息管理系統”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。