Issue
I'm having a problem with my code. I'm making a dealership website. I'm getting a null value for my attribute. My program runs through a foreach loop of my ArrayList on my JSP, I also have buttons on each car to purchase them. When the user chooses purchase on that item its suppose to fetch the car that they chose and forward it to a purchase page. But for some odd reason when I get to the purchase page the car value is null. Any help will be appreciated.
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/[email protected]/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/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
</body>
</html>
Solution
It's a lot of code to comprehend but I would say the problem is here:
<foreach>
<form>
<c:set var="purchaseCar" value="${car}" scope="application" />
<submit...
</form>
</foreach>
Session scope will be enough here. But the real problem is this is executed for all records and each is overriding the previous one. So the purchaseCar variable is modified on each iteration.
Usually in a form you want to have <input>
tags that sends the needed information with the request to the servlet. Usually that would be the car's ID :)
Answered By - Veselin Davidov
Answer Checked By - David Goodson (JavaFixing Volunteer)