package web;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
* Servlet implementation class ServletProduit
*/
@WebServlet("/ServletProduit")
public class ServletProduit extends HttpServlet {
private static final long serialVersionUID = 1L;
MetierProduitlmpl metier=new MetierProduitlmpl();
/**
* @see HttpServlet#HttpServlet()
*/
public ServletProduit() {
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
[Link]().getRequestDispatcher("/WEB-INF/[Link]").forward(requ
est, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String action = [Link]("action");
if ("addProduit".equals(action)) {
ajouterProduit(request, response);
} else if ("rechercher".equals(action)) {
chercherProduit(request, response);
} else if ("deleteProduit".equals(action)) {
int idProduit = [Link]([Link]("idProduit"));
// Suppression du produit
[Link](idProduit);
// Après suppression, actualiser la liste complète des produits
List<Produit> produits = [Link](""); // Liste vide
pour tout récupérer
ProduitModel model = new ProduitModel();
[Link](produits);
[Link]("modele", model);
[Link]("message", "Produit supprimé avec succès !");
}
// Rediriger vers la page des produits après chaque action
[Link]().getRequestDispatcher("/WEB-INF/[Link]").forward(requ
est, response);
}
private void ajouterProduit(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String nomProduit = [Link]("nomProduit");
String prixStr = [Link]("prix");
String quantiteStr = [Link]("quantite");
// Validation des champs
if (nomProduit != null && ![Link]() && prixStr != null &&
quantiteStr != null) {
try {
double prix = [Link](prixStr);
int quantite = [Link](quantiteStr);
Produit p = new Produit();
[Link](nomProduit);
[Link](prix);
[Link](quantite);
// Ajout du produit via la couche métier
[Link](p);
// Message de succès
[Link]("message", "Produit ajouté avec succès !");
} catch (NumberFormatException e) {
[Link]("message", "Erreur de format pour le prix ou
la quantité.");
}
} else {
[Link]("message", "Tous les champs sont requis.");
}
}
private void chercherProduit(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String motCle = [Link]("motCle");
ProduitModel mod = new ProduitModel();
[Link](motCle);
// Recherche des produits via la couche métier
List<Produit> produits = [Link](motCle);
[Link](produits);
[Link]("modele", mod);
}
private void supprimerProduit(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String idProduitStr = [Link]("idProduit");
if (idProduitStr != null && ![Link]()) {
try {
long idProduit = [Link](idProduitStr);
// Appel de la méthode pour supprimer le produit via la couche
métier
boolean isDeleted = [Link](idProduit);
if (isDeleted) {
[Link]("message", "Produit supprimé avec
succès !");
} else {
[Link]("message", "Erreur : produit
introuvable.");
}
} catch (NumberFormatException e) {
[Link]("message", "Erreur lors de la suppression : ID
invalide.");
}
} else {
[Link]("message", "Aucun produit sélectionné pour la
suppression.");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="[Link] prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Produits</title>
<!-- Bootstrap CSS -->
<link
href="[Link]
rel="stylesheet">
<script type="text/javascript">
// Fonction JavaScript pour la confirmation de suppression
function confirmDeletion(productId) {
var confirmation = confirm("Êtes-vous sûr de vouloir supprimer ce
produit ?");
if (confirmation) {
// Si l'utilisateur confirme, on soumet le formulaire
[Link]("deleteForm_" + productId).submit();
}
}
</script>
</head>
<body class="bg-light">
<div class="container py-5">
<h1 class="text-center mb-4">Liste des Produits</h1>
<!-- Formulaire de recherche -->
<form action="ServletProduit" method="post" class="mb-4">
<input type="hidden" name="action" value="rechercher" />
<div class="input-group">
<input type="text" name="motCle" value="${[Link]}"
class="form-control" placeholder="Rechercher...">
<button type="submit" class="btn btn-primary">OK</button>
</div>
</form>
<!-- Affichage des produits -->
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Nom</th>
<th scope="col">Prix</th>
<th scope="col">Quantité</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<c:forEach items="${[Link]}" var="p">
<tr>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>${[Link]}</td>
<td>
<!-- Formulaire pour supprimer un produit avec
confirmation -->
<form id="deleteForm_${[Link]}"
action="ServletProduit" method="post">
<input type="hidden" name="idProduit" value="$
{[Link]}" />
<input type="hidden" name="action"
value="deleteProduit" />
<input type="button" value="Supprimer" class="btn
btn-danger" onclick="confirmDeletion(${[Link]})" />
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- Formulaire d'ajout de produit -->
<div class="mt-5">
<h2 class="text-center mb-4">Ajouter un Produit</h2>
<form action="ServletProduit" method="post">
<div class="mb-3">
<label for="nomProduit" class="form-label">Nom du
Produit</label>
<input type="text" name="nomProduit" id="nomProduit"
class="form-control" required>
</div>
<div class="mb-3">
<label for="prix" class="form-label">Prix</label>
<input type="number" step="0.01" name="prix" id="prix"
class="form-control" required>
</div>
<div class="mb-3">
<label for="quantite" class="form-label">Quantité</label>
<input type="number" name="quantite" id="quantite" class="form-
control" required>
</div>
<button type="submit" class="btn btn-success" name="action"
value="addProduit">Ajouter</button>
</form>
</div>
</div>
<!-- Bootstrap JS (optional) -->
<script
src="[Link]
</script>
</body>
</html>