servlet 实现结算购物车商品添加到订单(记录项目笔记、代码不全 仅供参考)

本文记录了如何在Web项目中使用Servlet实现购物车结算功能。JSP页面负责展示购物车内容并传递商品数据到Servlet,Servlet则负责接收JSON数组,判断用户登录状态,并将数据处理成订单。
摘要由CSDN通过智能技术生成

JSP页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
  
      <%@page import="java.sql.PreparedStatement,petTest.bean.*"%>
      <%@page import="petTest.dao.QueryProduct,petTest.bean.Products"%>
      <%@ page import="java.util.*" %>
    <%@ page import="petTest.dao.JDBCTest, java.sql.SQLException,
	java.sql.Connection,java.sql.ResultSet,java.sql.PreparedStatement,java.sql.Statement,java.sql.ResultSet "%>
<jsp:directive.page import="java.util.List" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>


<style>
        * {
        margin: 0;
        padding: 0;
        font-family: "微软雅黑";
        list-style: none;
        color: #666;
        text-decoration: none;
        font-size: 14px;
    }
    body {
        background: #ffff;
        height: 100%;
    }
    .header{
    font-size: 12px;
    border-bottom: 2px solid #ff6700;
    background: #fff;
    color: #b0b0b0;
    position: relative;
    z-index: 20;
    height: 100px;
}
    .header .container {
    position: relative;
    width: 1226px;
    margin-right: auto;
    margin-left: auto;
}
 .header .container .header-logo {
    width: 93px;
    margin-top: 26px;
}
 
 
    .header-title {
    float: left;
    margin-top: 26px;
    font-size: 12px;
    }
    .topbar-info {
    margin-top: 30px;
    line-height: 40px;
}
    .link {
    padding: 0 5px;
    color: #757575;
    text-decoration: none;
    }
    .hid {
        overflow: hidden;
    }
    .left {
        float: left;
    }

    .car {
        width: 1240px;
        margin: 20px auto;
        background: #FFF;
    }
    .car .check{
        width: 50px;
 
    }
    .car .check i{
        color: #fff;
        display: inline-block;
 
        width: 18px;
        height: 18px;
        line-height: 18px;
        border: 1px solid #e0e0e0;
        margin-left: 24px;
        background-color: #fff;
        font-size: 16px;
        text-align: center;
        vertical-align: middle;
        position: relative;
        top: -1px;
        cursor: pointer;
        font-family: "iconfont";
    }
    .i_acity {
 
        border-color: #ff6700 !important;
        background-color: #ff6700 !important;
    }
    .car .img {
        width: 190px;
    }
    .car .img img {
        display: block;
        width: 80px;
        height: 80px;
        margin: 3px auto;
    }
    .car .name {
        width: 300px;
    }
    .car .name span {
        line-height: 1;
        margin-top: 8px;
        margin-bottom: 8px;
        font-size: 18px;
        font-weight: normal;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
    }
        .car .names {
        width: 300px;
    }
    .car .names span {
        line-height: 1;
        margin-top: 8px;
        margin-bottom: 8px;
        font-size: 18px;
        font-weight: normal;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
    }
    
    .car .price {
        width: 144px;
        text-align: right;
        padding-right: 84px;
    }
    .car .price span {
        color: #ff6700;
        font-size: 16px;
    }
    .car .number{
        width: 150px;
    }
    .car .subtotal{
        width: 130px;
 
    }
    .car .ctrl {
        width: 105px;
        padding-right:25px;
        text-align: center;
    }
    .car .ctrl a {
        font-size: 20px;
        cursor: pointer;
        display: block;
        width: 26px;
        height: 26px;
        margin: 30px auto;
        line-height: 26px;
    }
    .car .ctrl a:hover {
        color: #FFF;
        background: #ff6700;
        border-radius: 50%;
    }
    .head_row {
        height: 70px;
        line-height: 70px;
    }
    .head_row, .row {
        border-bottom: solid 1px #e0e0e0;
    }
    .row {
        height: 86px;
        line-height:86px;
        padding: 15px 0;
        margin: 0px;
    }
    #sum_area{
        width:1240px;
        height: 60px;
        background: white;
        margin: 20px auto;
    }
    #sum_area #pay{
        width:250px;
        height:60px;
        text-align: center;
        float: right;
        line-height: 60px;
        font-size: 19px;
        background: #FF4B00;
        color: white;
    }
    #sum_area #pay_amout{
        width:250px;
        height:60px;
        text-align: center;
        float: right;
        line-height: 60px;
        font-size: 16px;
        color:#FF4B00 ;
    }
    #sum_area #pay_amout #price_num{
        width:100px;
        height: 60px;
        font-size: 25px;
        color:#FF4B00 ;
    /*  float: left;*/
    }
 
.item_count_i{
        height: 85px;
        width:10%;
        /*border: 1px solid black;*/
        float: left;
        margin-right: 25px;
    }
    .num_count{
        width:150px;
        height:40px;
        border: 1.2px solid #E0E0E0;
        float:right;
        margin-top: 21px;
 
    }
    .count_i{
        width:30%;
        height:40px;
        line-height: 40px;
        float: left;
        text-align: center;
        font-size:21px;
        color: #747474;
    }
    .count_i:hover{
        width:30%;
        height:40px;
        line-height: 40px;
        float: left;
        text-align: center;
        font-size:21px;
        color: #747474;
        background: #E0E0E0;
        cursor: pointer;
    }
    .c_num{
        width:40%;
        height:40px;
        line-height: 40px;
        float: left;
        text-align: center;
        font-size:16px;
        color: #747474;
    }
    .count_d{
        width:30%;
        height:40px;
        line-height: 40px;
        float: left;
        text-align: center;
        font-size:25px;
        color: #747474;
    }
    .count_d:hover{
        width:30%;
        height:40px;
        line-height: 40px;
        float: left;
        text-align: center;
        font-size:25px;
        color: #747474;
        background: #E0E0E0;
        cursor: pointer;
    }
    .i_acity2 {
        border-color: #ff6700 !importan
实现购物车添加功能需要考虑以下几个方面: 1. 购物车数据的存储:可以使用 Session 对象来存储购物车数据,也可以将其存储到数据库等持久化存储中。 2. 添加商品购物车:需要获取商品的信息,然后将其添加购物车中。 3. 展示购物车内容:需要将购物车中的商品信息展示出来,可以使用 JSP 页面实现。 下面是一个使用 Servlet 实现购物车添加功能的示例代码,其中假设购物车中只有一种商品商品信息为商品名称和价格: ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AddToCartServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取商品信息 String itemName = request.getParameter("itemName"); double itemPrice = Double.parseDouble(request.getParameter("itemPrice")); // 从 Session 中获取购物车数据 HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute("cart"); if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } // 添加商品购物车 cart.addItem(itemName, itemPrice); // 跳转到购物车页面 response.sendRedirect("cart.jsp"); } } class ShoppingCart { private int itemCount; private double totalAmount; public void addItem(String itemName, double itemPrice) { // 添加商品购物车 itemCount++; totalAmount += itemPrice; } public int getItemCount() { return itemCount; } public double getTotalAmount() { return totalAmount; } } ``` 在上述代码中,我们使用了 Session 对象来存储购物车数据,当用户添加商品购物车时,我们从 Session 中获取购物车数据,如果购物车不存在,则创建一个新的购物车对象,并将其存储到 Session 中。然后将新的商品信息添加购物车中,最后跳转到购物车页面展示购物车内容。 使用 JSP 页面展示购物车内容的示例代码如下: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="ShoppingCart" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> </head> <body> <h1>购物车</h1> <table> <thead> <tr> <th>商品名称</th> <th>商品价格</th> </tr> </thead> <tbody> <% HttpSession session = request.getSession(); ShoppingCart cart = (ShoppingCart) session.getAttribute("cart"); if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } List<String> itemNames = new ArrayList<>(); List<Double> itemPrices = new ArrayList<>(); itemNames.add("商品A"); itemPrices.add(100.0); itemNames.add("商品B"); itemPrices.add(200.0); itemNames.add("商品C"); itemPrices.add(300.0); for (int i = 0; i < itemNames.size(); i++) { String itemName = itemNames.get(i); double itemPrice = itemPrices.get(i); cart.addItem(itemName, itemPrice); %> <tr> <td><%= itemName %></td> <td><%= itemPrice %></td> </tr> <% } %> </tbody> <tfoot> <tr> <td>总计</td> <td><%= cart.getTotalAmount() %></td> </tr> </tfoot> </table> </body> </html> ``` 在上述代码中,我们首先从 Session 中获取购物车数据,如果购物车不存在,则创建一个新的购物车对象,并将其存储到 Session 中。然后模拟了三个商品信息,并将其添加购物车中。最后展示购物车中的商品信息和总价。实际中,这些商品信息应该从数据库中获取。 希望这个示例能够帮助你理解如何使用 Servlet 实现购物车添加功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值