Frontend:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-
8" %>
<%@ page import="[Link].*, [Link]" %>
<%
List<Product> products = (List<Product>) [Link]("products");
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Consumer Page</title>
<link
href="[Link]
rel="stylesheet">
<style>
/* Body Background */
body {
background: white;
}
/* Glassmorphism Card */
.card {
border-radius: 15px;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
border: none;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
}
/* Product Title */
.card-title {
font-weight: bold;
font-size: 18px;
color: #333;
}
/* Price Text */
.card-subtitle {
font-size: 16px;
font-weight: 500;
color: #777;
}
/* Custom Buttons */
.btn-custom {
background: linear-gradient(to right, #ff9052, #ff6f61);
color: white;
font-weight: bold;
border-radius: 25px;
padding: 8px 15px;
transition: all 0.3s ease-in-out;
border: none;
}
.btn-custom:hover {
background: linear-gradient(to right, #ff6f61, #ff9052);
transform: scale(1.05);
color: white;
}
/* Quantity Input */
.quantity-input {
width: 25px;s
text-align: center;
font-weight: bold;
}
.cart-actions {
display: flex;
align-items: center;
}
.btn-green {
background: linear-gradient(45deg, #ff9052, #ff6f61);
color: white;
border: none;
}
.btn-green {
background: linear-gradient(45deg, #ff9052, #ff6f61);
color: white;
border: none;
}
.btn-green:hover {
background: linear-gradient(45deg, #ff6f61, #ff9052);
transform: scale(1.05);
}
.btn-yellow { background: linear-gradient(45deg, #ffcc00, #ffb300); color:
black; }
</style>
</head>
<body>
<%@ include file="[Link]" %>
<div class="container mt-5">
<h2 class="text-center fw-bold mb-4" style="color: #333;">Explore Our
Products</h2>
<div class="row">
<% if (products != null && ![Link]()) { %>
<% for (Product p : products) { %>
<div class="col-md-4 mb-4">
<div class="card p-4">
<div class="card-body text-center">
<h5 class="card-title"><%= p.getProduct_name()
%></h5>
<h6 class="card-subtitle mb-3">Price: ₹<%=
[Link]() %></h6>
<form action="CartController" method="post"
class="d-flex align-items-center justify-content-center gap-2">
<input type="hidden" name="product_id" value="<
%= p.getProduct_id() %>">
<input type="hidden" name="product_name"
value="<%= p.getProduct_name() %>">
<input type="hidden" name="price" value="<%=
[Link]() %>">
<input type="number" name="quantity"
placeholder="Qty" class="form-control quantity-input" value="1" min="1">
<button type="submit" class="btn btn-custom">🛒
Add to Cart</button>
</form>
</div>
</div>
</div>
<% } %>
<% } else { %>
<div class="col-12">
<p class="text-center text-muted fs-5">No products available at
the moment.</p>
</div>
<% } %>
</div>
<!-- Action Buttons -->
<div class="d-flex justify-content-center gap-3 mt-4">
<button class="btn btn-green fw-bold px-4 py-2 me-2"
onclick="[Link]='ConsumerOrderController'">
View Ordered Products
</button>
<button class="btn btn-green fw-bold px-4 py-2"
onclick="[Link]='ConsumerReportController'">
View Reported Products
</button>
</div>
</div>
<%@ include file="[Link]" %>
<script
src="[Link]
</script>
</body>
</html>
Middleware:
package db;
import [Link];
import [Link];
import [Link];
public class GetConnection {
public static Connection getConnection() {
Connection connection = null;
try {
[Link]("[Link]");
connection =
[Link]("jdbc:mysql://localhost:3307/db_1", "root", "pass");
} catch (ClassNotFoundException | SQLException e) {
[Link]();
}
return connection;
}
}
package model;
import [Link];
public class Product {
private int product_id;
private String product_name;
private int quantity;
private float price;
private int seller_id;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
[Link] = quantity;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
[Link] = price;
}
public int getSeller_id() {
return seller_id;
}
public void setSeller_id(int seller_id) {
this.seller_id = seller_id;
}
// Methods for CRUD operations
public void addProduct(Product product) {
new ProductImplementor().addProduct(product);
}
public void updateProduct(Product product) {
new ProductImplementor().updateProduct(product);
}
public void deleteProduct(int product_id) {
new ProductImplementor().deleteProduct(product_id);
}
}
package operations;
import [Link];
import [Link];
public interface ProductOperations {
void addProduct(Product product);
void updateProduct(Product product);
void deleteProduct(int product_id);
List<Product> getSellerProducts(int sellerID);
List<Product> showProducts();
}
package operationImplementor;
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class ProductImplementor implements ProductOperations {
public void addProduct(Product product) {
try (Connection conn = [Link]();
CallableStatement stmt = [Link]("{CALL addproduct(?, ?, ?,
?)}")) {
[Link](1, product.getProduct_name());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, product.getSeller_id());
[Link]();
} catch (SQLException e) {
[Link]();
}
}
public void updateProduct(Product product) {
try (Connection conn = [Link]();
CallableStatement stmt = [Link]("{CALL updateproduct(?, ?,
?, ?)}")) {
[Link](1, product.getProduct_id());
[Link](2, product.getProduct_name());
[Link](3, [Link]());
[Link](4, [Link]());
[Link]();
} catch (SQLException e) {
[Link]();
}
}
public void deleteProduct(int productId) {
try (Connection conn = [Link]();
CallableStatement stmt = [Link]("{CALL
deleteproduct(?)}")) {
[Link](1, productId);
[Link]();
} catch (SQLException e) {
[Link]();
}
}
@Override
public List<Product> getSellerProducts(int sellerID) {
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
List<Product> productList = new ArrayList<>();
try {
conn = [Link]();
cs = [Link]("{CALL GetSellerProducts(?)}");
[Link](1, sellerID);
rs = [Link]();
while ([Link]()) {
Product product = new Product();
product.setProduct_id([Link]("product_id"));
product.setProduct_name([Link]("product_name"));
[Link]([Link]("quantity"));
[Link]([Link]("price"));
product.setSeller_id([Link]("seller_id"));
[Link](product);
}
} catch (SQLException e) {
[Link]();
} finally {
try {
if (rs != null) [Link]();
if (cs != null) [Link]();
if (conn != null) [Link]();
} catch (SQLException e) {
[Link]();
}
}
return productList;
}
@Override
public List<Product> showProducts() {
List<Product> productList = new ArrayList<>();
Connection conn = null;
CallableStatement stmt = null;
ResultSet rs = null;
try {
conn = [Link]();
[Link]("Database Connection Successful!"); // Debugging
step
stmt = [Link]("{CALL view_products()}");
rs = [Link]();
while ([Link]()) {
Product product = new Product();
product.setProduct_id([Link]("product_id"));
product.setProduct_name([Link]("product_name"));
[Link]([Link]("quantity"));
[Link]([Link]("price"));
[Link](product);
}
[Link]("Fetched Products: " + [Link]()); //
Debugging step
} catch (SQLException e) {
[Link]();
} finally {
try {
if (rs != null) [Link]();
if (stmt != null) [Link]();
if (conn != null) [Link]();
} catch (SQLException e) {
[Link]();
}
}
return productList;
}
package controller;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/ProductController")
public class ProductController extends HttpServlet {
private static final long serialVersionUID = 1L;
private ProductImplementor implementor;
public void init() {
implementor = new ProductImplementor();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = [Link]("action");
switch (action) {
case "add":
addProduct(request, response);
break;
case "update":
updateProduct(request, response);
break;
default:
[Link]("[Link]");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = [Link]("action");
if ("delete".equals(action)) {
deleteProduct(request, response);
} else {
[Link]("[Link]");
}
}
private void addProduct(HttpServletRequest request, HttpServletResponse
response)
throws IOException {
try {
int sellerId = [Link]([Link]("seller_id"));
String productName = [Link]("product_name");
int quantity = [Link]([Link]("quantity"));
float price = [Link]([Link]("price"));
// Ensure productName is not null or empty
if (productName == null || [Link]().isEmpty()) {
[Link]("[Link]?error=Product name cannot be
empty");
return;
}
Product newProduct = new Product();
newProduct.setSeller_id(sellerId);
newProduct.setProduct_name(productName);
[Link](quantity);
[Link](price);
[Link](newProduct);
[Link]("[Link]?message=Product added successfully");
} catch (Exception e) {
[Link]();
[Link]("[Link]?error=Failed to add product");
}
}
private void updateProduct(HttpServletRequest request, HttpServletResponse
response)
throws IOException {
try {
int productId = [Link]([Link]("product_id"));
String productName = [Link]("product_name");
int quantity = [Link]([Link]("quantity"));
float price = [Link]([Link]("price"));
if (productName == null || [Link]().isEmpty()) {
[Link]("[Link]?error=Product name cannot be
empty");
return;
}
Product updatedProduct = new Product();
updatedProduct.setProduct_id(productId);
updatedProduct.setProduct_name(productName);
[Link](quantity);
[Link](price);
[Link](updatedProduct);
[Link]("[Link]?message=Product updated
successfully");
} catch (Exception e) {
[Link]();
[Link]("[Link]?error=Failed to update product");
}
}
private void deleteProduct(HttpServletRequest request, HttpServletResponse
response)
throws IOException {
try {
int productId = [Link]([Link]("product_id"));
Product product = new Product();
[Link](productId);
[Link]("[Link]?message=Product deleted
successfully");
} catch (Exception e) {
[Link]();
[Link]("[Link]?error=Failed to delete product");
}
}
}
Backend:
-- Procedure for view products
DELIMITER //
CREATE PROCEDURE view_products()
BEGIN
SELECT product_id, product_name, quantity, price
FROM products;
END;
//
DELIMITER ;
call view_products();