update jakarta

This commit is contained in:
2025-10-03 14:58:15 +02:00
parent 4634cfd4c2
commit c8f8c628d9
15 changed files with 401 additions and 39 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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 <code>GET</code> and <code>POST</code>
* 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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet DeconnexionServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet DeconnexionServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> 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 <code>POST</code> 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";
}// </editor-fold>
}

View File

@@ -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 <code>GET</code> and <code>POST</code>
* 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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ModifClientServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ModifClientServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> 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 <code>POST</code> 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";
}// </editor-fold>
}

View File

@@ -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;
}

View File

@@ -26,8 +26,5 @@
</c:choose>
</c:when>
</c:choose>
<p>
${sessionScope.isAuthentified}
</p>
</body>
</html>

View File

@@ -1,6 +1,6 @@
<%-- any content can be specified here e.g.: --%>
<%@ page pageEncoding="UTF-8" %>
<form method="POST" action="authentification">
<form class="nouveau-client" method="POST" action="authentification">
<label>Nom d'utilisateur: <input name="pseudo" type='text'></label><br>
<label>Mot de passe: <input name='mdp' type='password'></label><br>
<button type="submit">Connexion</button>

View File

@@ -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" %>
<nav>
<ul>
<li><a href="/Test2Jakarta/Accueil">Accueil</a></li>
<li><a href="/Test2Jakarta/ListeClients">Liste des clients</a></li>
<li><a href="/Test2Jakarta/NouveauClient">Nouveau client</a></li>
<c:choose>
<c:when test="${sessionScope.isAdmin}">
<li><a href="/Test2Jakarta/NouveauClient">Nouveau client</a></li>
</c:when>
</c:choose>
<c:choose>
<c:when test="${sessionScope.isAuthentified}">
<li><a href="/Test2Jakarta/deconnexion">Deconnexion</a></li>
</c:when>
</c:choose>
</ul>
</nav>

View File

@@ -12,10 +12,53 @@
<body>
<%@include file="jspf/menu.jspf" %>
<h1>Liste des clients</h1>
<ul>
<c:forEach var="client" items="${clients}">
<li>${client.getNom()}</li>
</c:forEach>
</ul>
<c:if test="${sessionScope.isAdmin}">
<c:choose>
<c:when test="${modification == true}">
<p>${modification_message}</p>
</c:when>
<c:when test="${!modification == false}">
<p>$modification_message</p>
</c:when>
</c:choose>
</c:if>
<table>
<tr>
<th>ID</th>
<th>Nom</th>
<th>Prénom</th>
<th>Adresse mail</th>
<c:choose>
<c:when test="${sessionScope.isAdmin}">
<th>Modification</th>
</c:when>
</c:choose>
</tr>
<c:forEach var="client" items="${clients}">
<c:choose>
<c:when test="${sessionScope.isAdmin}">
<tr>
<td>${client.getId()}</td>
<form method="post" action="modifierClient">
<input type="hidden" name="clientId" value="${client.getId()}">
<td><input type="text" name="clientNom" placeholder="${client.getNom()}"></td>
<td><input type="text" name="clientPrenom" placeholder="${client.getPrenom()}"></td>
<td><input type="text" name="clientMail" placeholder="${client.getMail()}"></td>
<td><button type="submit">Modifier</button></td>
</form>
</tr>
</c:when>
<c:when test="${!sessionScope.isAdmin}">
<tr>
<td>${client.getId()}</td>
<td>${client.getNom()}</td>
<td>${client.getPrenom()}</td>
<td>${client.getMail()}</td>
</tr>
</c:when>
</c:choose>
</c:forEach>
</table>
</body>
</html>

View File

@@ -12,7 +12,7 @@
<body>
<%@include file="jspf/menu.jspf" %>
<h1>Création dun nouveau client</h1>
<form method="post" action="NouveauClient">
<form class="nouveau-client" method="post" action="NouveauClient">
<fieldset>
<legend>Client :</legend>
<label>Nom :</label>

View File

@@ -16,10 +16,14 @@
<servlet-name>AuthentifServlet</servlet-name>
<servlet-class>com.test.servlets.AuthentifServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AccueilServlet</servlet-name>
<url-pattern>/Accueil</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DeconnexionServlet</servlet-name>
<servlet-class>com.test.servlets.DeconnexionServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ModifClientServlet</servlet-name>
<servlet-class>com.test.servlets.ModifClientServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AccueilServlet</servlet-name>
<url-pattern>/Accueil</url-pattern>
@@ -36,9 +40,20 @@
<servlet-name>AuthentifServlet</servlet-name>
<url-pattern>/authentification</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeconnexionServlet</servlet-name>
<url-pattern>/deconnexion</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ModifClientServlet</servlet-name>
<url-pattern>/ModifClientServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>Accueil</welcome-file>
</welcome-file-list>
</web-app>

View File

@@ -1,12 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1><a href="/Test2Jakarta/Accueil">Accueil</a></h1>
<h1><a href="/Test2Jakarta/NouveauClient">Nouveau client</a></h1>
<h1><a href="/Test2Jakarta/ListeClients">Liste clients</a></h1>
</body>
</html>

View File

@@ -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;
}