<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="com.entity.Customer" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="com.helper.DBHelper" %> <%@ page import="java.sql.*" %> Shopping Cart <% if (session.getAttribute("customer") != null) { Customer customer = (Customer) session.getAttribute("customer"); %>
Welcome, <%= customer.getName() %> to your Shopping Cart
<% String sql = "SELECT * FROM CART WHERE customerId = ?"; try (PreparedStatement pstmt = DBHelper.getPreparedStatement(sql)) { pstmt.setInt(1, customer.getCustomerId()); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { int productId = rs.getInt("productId"); String productQuery = "SELECT * FROM PRODUCTS WHERE productId = ?"; try (PreparedStatement productStmt = DBHelper.getPreparedStatement(productQuery)) { productStmt.setInt(1, productId); ResultSet productRs = productStmt.executeQuery(); if (productRs.next()) { %>
" alt="Product Image">

<%= productRs.getString("productName") %>

<%= productRs.getString("productDescription") %>

<%= productRs.getDouble("productPrice") %> per kg

<%= rs.getInt("quantity") %>
<% } } } } catch (SQLException e) { out.println("Error: " + e.getMessage()); } %>
<% } else { response.sendRedirect("LoginPage.jsp"); } %> @WebServlet("/UpdateCartServlet") public class UpdateCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int productId = Integer.parseInt(request.getParameter("productId")); int quantity = Integer.parseInt(request.getParameter("quantity")); // Get the customer ID from the session HttpSession session = request.getSession(); Customer customer = (Customer) session.getAttribute("customer"); if (customer != null) { int customerId = customer.getCustomerId(); String updateQuery = "UPDATE CART SET quantity = ? WHERE productId = ? AND customerId = ?"; try (Connection conn = DBHelper.getConnection(); PreparedStatement pstmt = conn.prepareStatement(updateQuery)) { pstmt.setInt(1, quantity); pstmt.setInt(2, productId); pstmt.setInt(3, customerId); int rowsUpdated = pstmt.executeUpdate(); if (rowsUpdated > 0) { response.setStatus(HttpServletResponse.SC_OK); } else { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } catch (SQLException e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); e.printStackTrace(); } } else { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); } } }