From c8f8c628d979609ab520ddc51f731be6920def26 Mon Sep 17 00:00:00 2001 From: "steve.maingana" Date: Fri, 3 Oct 2025 14:58:15 +0200 Subject: [PATCH] update jakarta --- src/main/java/com/test/bdd/ClientMysql.java | 21 ++++ src/main/java/com/test/bdd/Connexion.java | 2 +- .../java/com/test/forms/ModifClientForm.java | 44 ++++++++ .../com/test/servlets/AuthentifServlet.java | 2 +- .../com/test/servlets/DeconnexionServlet.java | 97 +++++++++++++++++ .../com/test/servlets/ModifClientServlet.java | 102 ++++++++++++++++++ .../com/test/servlets/NouveauServlet.java | 4 +- src/main/webapp/WEB-INF/accueilVue.jsp | 3 - src/main/webapp/WEB-INF/jspf/login.jspf | 2 +- src/main/webapp/WEB-INF/jspf/menu.jspf | 13 ++- src/main/webapp/WEB-INF/listeClientsVue.jsp | 53 ++++++++- src/main/webapp/WEB-INF/nouveauClientVue.jsp | 2 +- src/main/webapp/WEB-INF/web.xml | 23 +++- src/main/webapp/index.html | 12 --- src/main/webapp/style/style.css | 60 +++++++++-- 15 files changed, 401 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/test/forms/ModifClientForm.java create mode 100644 src/main/java/com/test/servlets/DeconnexionServlet.java create mode 100644 src/main/java/com/test/servlets/ModifClientServlet.java delete mode 100644 src/main/webapp/index.html diff --git a/src/main/java/com/test/bdd/ClientMysql.java b/src/main/java/com/test/bdd/ClientMysql.java index 0a69050..e98c643 100644 --- a/src/main/java/com/test/bdd/ClientMysql.java +++ b/src/main/java/com/test/bdd/ClientMysql.java @@ -82,4 +82,25 @@ public class ClientMysql { } return id; } + + public boolean update(int id, String nom, String prenom, String mail) { + String sql = "UPDATE client SET nom=?, prenom=?, mail=? WHERE id=?"; + int update = 0; + try { + PreparedStatement preparedStmt = laConnexion.prepareStatement(sql); + + preparedStmt.setString(1, nom); + preparedStmt.setString(2, prenom); + preparedStmt.setString(3, mail); + preparedStmt.setInt(4, id); + + update = preparedStmt.executeUpdate(); + } catch (SQLException ex) { + System.out.println("SQLException:"+ex.getMessage()); + System.out.println("SQLState:"+ex.getSQLState()); + System.out.println("code Erreur:"+ex.getErrorCode()); + } + + return update > 0; + } } diff --git a/src/main/java/com/test/bdd/Connexion.java b/src/main/java/com/test/bdd/Connexion.java index b50fcf6..393a29f 100644 --- a/src/main/java/com/test/bdd/Connexion.java +++ b/src/main/java/com/test/bdd/Connexion.java @@ -40,7 +40,7 @@ public class Connexion { } catch (Exception e) { e.printStackTrace(); - } + } } /** diff --git a/src/main/java/com/test/forms/ModifClientForm.java b/src/main/java/com/test/forms/ModifClientForm.java new file mode 100644 index 0000000..2709187 --- /dev/null +++ b/src/main/java/com/test/forms/ModifClientForm.java @@ -0,0 +1,44 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.test.forms; + +import com.test.bdd.ClientMysql; +import jakarta.servlet.http.HttpServletRequest; + +/** + * + * @author steve.maingana + */ +public class ModifClientForm { + public String resultat; + + public ModifClientForm() {}; + + public String getResultat() { + return resultat; + } + + public void setResultat(String resultat) { + this.resultat = resultat; + } + + public boolean modifierClient(HttpServletRequest request) { + ClientMysql clientDB = new ClientMysql(); + String id = request.getParameter("clientId"); + String nom = request.getParameter("clientNom"); + String prenom = request.getParameter("clientPrenom"); + String mail = request.getParameter("clientMail"); + + boolean modification = clientDB.update(Integer.parseInt(id), nom, prenom, mail); + + if (modification) { + this.setResultat("Le client N°"+id+" a été modifié avec succès"); + } else { + this.setResultat("Je n'ai pas pu modifier le client N°"+id); + } + + return modification; + } +} diff --git a/src/main/java/com/test/servlets/AuthentifServlet.java b/src/main/java/com/test/servlets/AuthentifServlet.java index e77abdc..af4f817 100644 --- a/src/main/java/com/test/servlets/AuthentifServlet.java +++ b/src/main/java/com/test/servlets/AuthentifServlet.java @@ -88,7 +88,7 @@ public class AuthentifServlet extends HttpServlet { if (!pseudo.isBlank() && !mdp.isBlank()) { if (authentification.existeUser(request)) { boolean isAdmin = authentification.controlerAdmin(request); - request.setAttribute("admin", isAdmin); + maSession.setAttribute("isAdmin", isAdmin); maSession.setAttribute("isAuthentified", true); } } diff --git a/src/main/java/com/test/servlets/DeconnexionServlet.java b/src/main/java/com/test/servlets/DeconnexionServlet.java new file mode 100644 index 0000000..2877889 --- /dev/null +++ b/src/main/java/com/test/servlets/DeconnexionServlet.java @@ -0,0 +1,97 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template + */ +package com.test.servlets; + +import java.io.IOException; +import java.io.PrintWriter; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +/** + * + * @author steve.maingana + */ + +@WebServlet(name = "DeconnexionServlet", urlPatterns = {"/deconnexion"}) + +public class DeconnexionServlet extends HttpServlet { + + /** + * Processes requests for both HTTP GET and POST + * methods. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + protected void processRequest(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + response.setContentType("text/html;charset=UTF-8"); + try (PrintWriter out = response.getWriter()) { + /* TODO output your page here. You may use following sample code. */ + out.println(""); + out.println(""); + out.println(""); + out.println("Servlet DeconnexionServlet"); + out.println(""); + out.println(""); + out.println("

Servlet DeconnexionServlet at " + request.getContextPath() + "

"); + out.println(""); + out.println(""); + } + } + + // + /** + * Handles the HTTP GET method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + HttpSession maSession = request.getSession(); + boolean isAuthentified = (maSession.getAttribute("isAuthentified") != null) ? (boolean) maSession.getAttribute("isAuthentified") : false; + if (isAuthentified) { + maSession.removeAttribute("isAuthentified"); + maSession.removeAttribute("isAdmin"); + } + + response.sendRedirect("/Test2Jakarta/Accueil"); + } + + /** + * Handles the HTTP POST method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + processRequest(request, response); + } + + /** + * Returns a short description of the servlet. + * + * @return a String containing servlet description + */ + @Override + public String getServletInfo() { + return "Short description"; + }// + +} diff --git a/src/main/java/com/test/servlets/ModifClientServlet.java b/src/main/java/com/test/servlets/ModifClientServlet.java new file mode 100644 index 0000000..2242e0f --- /dev/null +++ b/src/main/java/com/test/servlets/ModifClientServlet.java @@ -0,0 +1,102 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template + */ +package com.test.servlets; + +import com.test.forms.ModifClientForm; +import java.io.IOException; +import java.io.PrintWriter; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +/** + * + * @author steve.maingana + */ +@WebServlet(name = "ModifClientServlet", urlPatterns = {"modifierClient"}) +public class ModifClientServlet extends HttpServlet { + + /** + * Processes requests for both HTTP GET and POST + * methods. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + protected void processRequest(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + response.setContentType("text/html;charset=UTF-8"); + try (PrintWriter out = response.getWriter()) { + /* TODO output your page here. You may use following sample code. */ + out.println(""); + out.println(""); + out.println(""); + out.println("Servlet ModifClientServlet"); + out.println(""); + out.println(""); + out.println("

Servlet ModifClientServlet at " + request.getContextPath() + "

"); + out.println(""); + out.println(""); + } + } + + // + /** + * Handles the HTTP GET method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + processRequest(request, response); + } + + /** + * Handles the HTTP POST method. + * + * @param request servlet request + * @param response servlet response + * @throws ServletException if a servlet-specific error occurs + * @throws IOException if an I/O error occurs + */ + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + HttpSession maSession = request.getSession(); + boolean isAuthentified = (maSession.getAttribute("isAuthentified") != null) ? (boolean) maSession.getAttribute("isAuthentified") : false; + boolean isAdmin = (maSession.getAttribute("isAdmin") != null) ? (boolean) maSession.getAttribute("isAdmin") : false; + if (!isAuthentified || !isAdmin) { + response.sendRedirect("/Test2Jakarta/Accueil"); + return; + } + + ModifClientForm modification = new ModifClientForm(); + boolean modifClient = modification.modifierClient(request); + request.setAttribute("modification", modifClient); + request.setAttribute("modification_message", modification.getResultat()); + + response.sendRedirect("/Test2Jakarta/ListeClients"); + } + + /** + * Returns a short description of the servlet. + * + * @return a String containing servlet description + */ + @Override + public String getServletInfo() { + return "Short description"; + }// + +} diff --git a/src/main/java/com/test/servlets/NouveauServlet.java b/src/main/java/com/test/servlets/NouveauServlet.java index 6a417d0..9458a3a 100644 --- a/src/main/java/com/test/servlets/NouveauServlet.java +++ b/src/main/java/com/test/servlets/NouveauServlet.java @@ -62,10 +62,10 @@ public class NouveauServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // processRequest(request, response); HttpSession maSession = request.getSession(); boolean isAuthentified = (maSession.getAttribute("isAuthentified") != null) ? (boolean) maSession.getAttribute("isAuthentified") : false; - if (!isAuthentified) { + boolean isAdmin = (maSession.getAttribute("isAdmin") != null) ? (boolean) maSession.getAttribute("isAdmin") : false; + if (!isAuthentified || !isAdmin) { response.sendRedirect("/Test2Jakarta/Accueil"); return; } diff --git a/src/main/webapp/WEB-INF/accueilVue.jsp b/src/main/webapp/WEB-INF/accueilVue.jsp index 4e1694b..ea173e5 100644 --- a/src/main/webapp/WEB-INF/accueilVue.jsp +++ b/src/main/webapp/WEB-INF/accueilVue.jsp @@ -26,8 +26,5 @@ -

- ${sessionScope.isAuthentified} -

diff --git a/src/main/webapp/WEB-INF/jspf/login.jspf b/src/main/webapp/WEB-INF/jspf/login.jspf index 4e625a9..a0c1782 100644 --- a/src/main/webapp/WEB-INF/jspf/login.jspf +++ b/src/main/webapp/WEB-INF/jspf/login.jspf @@ -1,6 +1,6 @@ <%-- any content can be specified here e.g.: --%> <%@ page pageEncoding="UTF-8" %> -
+

diff --git a/src/main/webapp/WEB-INF/jspf/menu.jspf b/src/main/webapp/WEB-INF/jspf/menu.jspf index e6a3aaf..d2a03ef 100644 --- a/src/main/webapp/WEB-INF/jspf/menu.jspf +++ b/src/main/webapp/WEB-INF/jspf/menu.jspf @@ -1,9 +1,20 @@ <%-- any content can be specified here e.g.: --%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page pageEncoding="UTF-8" %> \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/listeClientsVue.jsp b/src/main/webapp/WEB-INF/listeClientsVue.jsp index adc2ab6..78c9629 100644 --- a/src/main/webapp/WEB-INF/listeClientsVue.jsp +++ b/src/main/webapp/WEB-INF/listeClientsVue.jsp @@ -12,10 +12,53 @@ <%@include file="jspf/menu.jspf" %>

Liste des clients

- + + + +

${modification_message}

+
+ +

$modification_message

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDNomPrénomAdresse mailModification
${client.getId()}
${client.getId()}${client.getNom()}${client.getPrenom()}${client.getMail()}
diff --git a/src/main/webapp/WEB-INF/nouveauClientVue.jsp b/src/main/webapp/WEB-INF/nouveauClientVue.jsp index a088d09..b460690 100644 --- a/src/main/webapp/WEB-INF/nouveauClientVue.jsp +++ b/src/main/webapp/WEB-INF/nouveauClientVue.jsp @@ -12,7 +12,7 @@ <%@include file="jspf/menu.jspf" %>

Création d’un nouveau client

-
+
Client : diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 9d75088..bafd09d 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -16,10 +16,14 @@ AuthentifServlet com.test.servlets.AuthentifServlet - - AccueilServlet - /Accueil - + + DeconnexionServlet + com.test.servlets.DeconnexionServlet + + + ModifClientServlet + com.test.servlets.ModifClientServlet + AccueilServlet /Accueil @@ -36,9 +40,20 @@ AuthentifServlet /authentification + + DeconnexionServlet + /deconnexion + + + ModifClientServlet + /ModifClientServlet + 30 + + Accueil + diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html deleted file mode 100644 index 6f4fe0b..0000000 --- a/src/main/webapp/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - Start Page - - - -

Accueil

-

Nouveau client

-

Liste clients

- - diff --git a/src/main/webapp/style/style.css b/src/main/webapp/style/style.css index ecb1045..d821ffa 100644 --- a/src/main/webapp/style/style.css +++ b/src/main/webapp/style/style.css @@ -41,18 +41,62 @@ h1 { font-weight: bold; } -form { - text-align: center; - margin: 8rem; +.nouveau-client { + max-width: 500px; + margin: 8rem auto; padding: 2rem; background-color: var(--secondary-color); - border-radius: 8px; - border-style: none; + border-radius: 10px; + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); + text-align: center; } -form label { - margin: auto; - padding-bottom: 1rem; +.nouveau-client label { + display: block; + margin-bottom: 1rem; + color: #fff; + font-weight: 600; + font-size: 1rem; +} + +.nouveau-client input[type="submit"] { + display: block; + margin: 2rem auto 0 auto; /* espace au-dessus + centrage horizontal */ + padding: 0.6rem 1.5rem; + border: none; + border-radius: 5px; + background-color: var(--primary-color, #3498db); color: white; font-weight: bold; + font-size: 1rem; + cursor: pointer; + transition: background-color 0.3s ease; +} + +.nouveau-client input[type="submit"]:hover { + background-color: var(--primary-color-hover, #2980b9); +} + +table { + width: 80%; + margin: 0 auto; + border-collapse: collapse; + box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); + background-color: white; +} + +th, td { + padding: 12px 15px; + text-align: left; + border-bottom: 1px solid #ddd; +} + +th { + background-color: var(--primary-color); + color: white; + font-weight: normal; +} + +tr:hover { + background-color: #f1f1f1; } \ No newline at end of file