Java Servlet 空属性

Java Servlet Null attributes

我的代码有问题。我正在制作一个经销商网站。我的属性得到空值。我的程序在我的 JSP 上通过 ArrayList 的 foreach 循环运行,我在每辆车上也有购买它们的按钮。当用户选择购买该商品时,它假设获取他们选择的汽车并将其转发到购买页面。但出于某种奇怪的原因,当我进入购买页面时,汽车价值为空。任何帮助将不胜感激。

Servlet:

package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dealership.Car;
import com.dealership.Inventory;

/**
 * Servlet implementation class PurchaseServlet
 */
@WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PurchaseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        
    
    
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        HttpSession session = request.getSession(true);
        
        
        Car purchaseCar = (Car)session.getAttribute("purchaseCar");
        Inventory inventory = new Inventory();
              if(purchaseCar == null) {
                    purchaseCar = new Car(); 
                    
              
              }
          if (request.getParameter("purchase") != null) {             
              inventory.test(purchaseCar);
          }
          session.setAttribute("purchaseCar", purchaseCar);
        
         
            
        RequestDispatcher rs = request.getRequestDispatcher("purchase.jsp");
        rs.forward(request, response);
        
    }

}

JSP:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<jsp:include page="/HomepageServlet" />


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet"
    href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
    integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
    crossorigin="anonymous">

<!-- Local CSS -->
<link rel="stylesheet" href="styles.css" />

<!-- Link to font -->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link
    href="https://fonts.googleapis.com/css2?family=Cinzel:wght@600&display=swap"
    rel="stylesheet">



<title> Tropical Auto</title>
</head>
<body>
    <div>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="#">Nick's Tropical Autocenter</a> <a
                class="navbar-brand" href="#"> <img src="mango.svg" width="30"
                height="30" alt="" loading="lazy">
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse"
                data-target="#navbarNav" aria-controls="navbarNav"
                aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">

                <ul class="navbar-nav">
                    <li class="nav-item active"><a class="nav-link"
                        href="index.jsp"> INVENTORY <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item"><a class="nav-link" href="usedcars.jsp">USED
                            INVENTORY</a></li>
                    <li class="nav-item"><a class="nav-link" href="specials.jsp">SPECIALS</a>
                    </li>
                    <li class="nav-item"><a class="nav-link" href="#">ABOUT US</a>
                    </li>
                </ul>
                <form class="d-flex" action="SearchCarsServlet" method="post">
                    <input class="form-control me-2" type="search" name="searchInput"
                        placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-success" type="submit">Search</button>
                </form>
            </div>
        </nav>
    </div>
    
    <div class=album py-5bg-light">
    
        <div class="container">
            <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
                
                <c:forEach var="car" items="${inventory.carList}">
                    
                    <c:if
                        test="${car.used == false && car.residence < 120 && car.purchased == false}">
                        <div class="col">
                            <div class="card" style="width: 18rem;">
                                
                                <img src=${car.image } class="card-img-top" alt="...">
                                <div class="card-body">
                                    <p><span class="mandatory">${car.make}${car.model}</span></p>
                                    <p class="card-text">${car.description}
                                </div>
                                
                                <div class="d-flex justify-content-center align-items-center">
                                    <form action="PurchaseServlet" method="POST">
                                    <c:set var="purchaseCar" value="${car}" scope="application" />
                                    <button type="submit" name="purchase" class="btn btn-success">Purchase</button>
                                    </form> 
                                    <div class="dropdown">
                                        <a class="btn btn-secondary dropdown-toggle" href="#"
                                            role="button" id="dropdownMenuLink" data-toggle="dropdown"
                                            aria-haspopup="true" aria-expanded="false"> Details </a>
                                        <div class="dropdown-menu details-button"
                                            aria-labelledby="dropdownMenuLink">
                                            <a class="dropdown-item" href="#">Make: ${car.make}</a> <a
                                                class="dropdown-item" href="#">Model: ${car.model}</a> <a
                                                class="dropdown-item" href="#">Year: ${car.year}</a> <a
                                                class="dropdown-item" href="#">Mileage: ${car.mileage}</a> <a
                                                class="dropdown-item" href="#">Price: $${car.price}</a>                     
                                        </div>
                                    </div>
                                    
                                </div>
                        
                            </div>
                                
                        </div>
                        
                    </c:if>
                
                </c:forEach>
                
            </div>
        
        </div>
    
    </div>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script
        src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
        crossorigin="anonymous"></script>

</body>
</html>

需要理解的代码很多,但我想说问题出在这里:

<foreach>
  <form>
    <c:set var="purchaseCar" value="${car}" scope="application" />
 <submit...
</form>
</foreach>

会话范围在这里就足够了。但真正的问题是,这是针对所有记录执行的,并且每条记录都覆盖了前一条记录。因此每次迭代都会修改 purchaseCar 变量。

通常在表单中,您希望有 <input> 标记将所需信息与请求一起发送到 servlet。通常这就是汽车的 ID :)