diff --git a/src/java/bdd/PompierMySql.java b/src/java/bdd/PompierMySql.java index 1aaa1ca..4a3af80 100644 --- a/src/java/bdd/PompierMySql.java +++ b/src/java/bdd/PompierMySql.java @@ -6,12 +6,17 @@ package bdd; import com.test.beans.Pompier; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import util.MD5; /** * @@ -19,7 +24,7 @@ import java.util.ArrayList; */ public class PompierMySql { - private Connection theConnection; + private Connection theConnection; private Pompier unPompier; /** @@ -31,94 +36,163 @@ public class PompierMySql { "admin",// user "minda"); // mot de passe ClientMysql() { } - - /** - * Creation du pompier passé en paramètre dans la table pompier - * Requête non préparée + + /** + * Recherche de tous les clients + * + * @return collection de clients + */ + public ArrayList readAll() { + ArrayList lesPompiers = new ArrayList<>(); + + try { + Statement stmt = theConnection.createStatement(); + ResultSet resultQ = null; + resultQ = stmt.executeQuery("SELECT * FROM pompier"); + while (resultQ.next()) { + unPompier = new Pompier(resultQ.getInt("id"), + resultQ.getInt("idCaserne"), + resultQ.getString("nom"), + resultQ.getString("prenom"), + resultQ.getInt("statut"), + resultQ.getString("mail"), + resultQ.getString("login"), + resultQ.getString("mdp"), + resultQ.getInt("adrNo"), + resultQ.getString("adrRue"), + resultQ.getString("adrCP"), + resultQ.getString("adrVille"), + resultQ.getInt("grade"), + resultQ.getString("commentaire") + ); + lesPompiers.add(unPompier); + } + resultQ.close(); + stmt.close(); + //theConnection.close(); + } catch (SQLException ex) { + System.out.println("SQLException : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + + return lesPompiers; + } + + /** + * Creation du pompier passé en paramètre dans la table pompier Requête non + * préparée + * * @param p objet de type Pompier (sans identifiant) * @return int : id du Pompier créé */ public int create(Pompier p) { int id = -1; - try { + try { Statement stmt = theConnection.createStatement(); int status = stmt.executeUpdate( - "INSERT INTO pompier (idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade) " - + "VALUES ('" + p.getIdCaserne() + "', '" - + p.getNom() + "', '" - + p.getPrenom() + "', '" - + p.getStatut() + "', " - + p.getMail() + ", '" - + p.getLogin() + ", '" - + p.getMdp() + ", '" - + p.getAdrNo() + ", '" - + p.getAdrRue() + "', '" - + p.getAdrCP() + "', '" - + p.getAdrVille() + "', '" - + p.getGrade()+ "' );", - Statement.RETURN_GENERATED_KEYS); - - - + "INSERT INTO pompier (idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade) " + + "VALUES ('" + p.getIdCaserne() + "', '" + + p.getNom() + "', '" + + p.getPrenom() + "', '" + + p.getStatut() + "', " + + p.getMail() + ", '" + + p.getLogin() + ", '" + + p.getMdp() + ", '" + + p.getAdrNo() + ", '" + + p.getAdrRue() + "', '" + + p.getAdrCP() + "', '" + + p.getAdrVille() + "', '" + + p.getGrade() + "' );", + Statement.RETURN_GENERATED_KEYS); + // Recherche de l'identifiant du pompier créé if (status > 0) { ResultSet result = stmt.getGeneratedKeys(); if (result.first()) { - id = result.getInt(1); + id = result.getInt(1); } - } + } } catch (SQLException ex) { System.out.println("SQLException : " + ex.getMessage()); System.out.println("SQLState : " + ex.getSQLState()); System.out.println("Code erreur : " + ex.getErrorCode()); - } + } return id; } - - /** - * Creation du pompier passé en paramètre dans la table pompier - * Requête préparée + + /** + * Creation du pompier passé en paramètre dans la table pompier Requête + * préparée + * * @param p objet de type Pompier (sans identifiant) * @return int : id du pompier créé */ public int createRP(Pompier p) { int id = -1; - try { + try { PreparedStatement stmt = null; - String sql = "INSERT INTO pompier(idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade) " - + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);"; + String sql = "INSERT INTO pompier(idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);"; stmt = theConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, p.getIdCaserne()); - stmt.setString(2, p.getNom()); + stmt.setString(2, p.getNom()); stmt.setString(3, p.getPrenom()); stmt.setInt(4, p.getStatut()); - stmt.setString(5,p.getMail() ); - stmt.setString(6,p.getLogin() ); - stmt.setString(7,p.getMdp() ); + stmt.setString(5, p.getMail()); + stmt.setString(6, p.getLogin()); + stmt.setString(7, p.getMdp()); stmt.setInt(8, p.getAdrNo()); stmt.setString(9, p.getAdrRue()); stmt.setString(10, p.getAdrCP()); stmt.setString(11, p.getAdrVille()); stmt.setInt(12, p.getGrade()); - + System.out.println("Requête : " + stmt.toString()); int status = stmt.executeUpdate(); - - + // Recherche de l'identifiant du client créé if (status > 0) { ResultSet result = stmt.getGeneratedKeys(); if (result.first()) { - id = result.getInt(1); + id = result.getInt(1); } - } + } } catch (SQLException ex) { System.out.println("SQLException : " + ex.getMessage()); System.out.println("SQLState : " + ex.getSQLState()); System.out.println("Code erreur : " + ex.getErrorCode()); - } + } return id; } - -} \ No newline at end of file + public boolean isAuthentified(HttpServletRequest request) { + + boolean resultat = false; + String login = request.getParameter("ztPseudo"); + String mdp = request.getParameter("ztMDP"); + String mdpChiffre = MD5.encode(request.getParameter("ztMDP")); + String sql = "SELECT * FROM pompier WHERE login='" + login + "' AND mdp='" + mdpChiffre + "';"; + System.out.println("sql:"+sql); + try { + Statement stmt = theConnection.createStatement(); + ResultSet resultQ = null; + resultQ = stmt.executeQuery(sql); + resultat = (resultQ.next()); +// if(resultat){ +// Pompier lePompier = new Pompier( +// resultQ.getInt("id"), +// resultQ.getInt("idCAserne"), +// +// ); +// HttpSession maSession = request.getSession(); +// maSession.setAttribute("lePompier", lePompier); +// } + + } catch (SQLException ex) { + Logger.getLogger(PompierMySql.class.getName()).log(Level.SEVERE, null, ex); + } + return resultat; + } + +} diff --git a/src/java/com/test/beans/Pompier.java b/src/java/com/test/beans/Pompier.java index 6771412..bcbedb6 100644 --- a/src/java/com/test/beans/Pompier.java +++ b/src/java/com/test/beans/Pompier.java @@ -25,13 +25,14 @@ public class Pompier { private String adrCP; private String adrVille; private int grade; + private String commentaire; - public Pompier(int id, int idCaserne, String nom, String prenom, int statut, String mail, String login, String mdp, int adrNo, String adrRue, String adrCP, String adrVille, int grade) { - this(idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade); + public Pompier(int id, int idCaserne, String nom, String prenom, int statut, String mail, String login, String mdp, int adrNo, String adrRue, String adrCP, String adrVille, int grade, String commentaire) { + this(idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade, commentaire); this.id = id; } - - public Pompier(int idCaserne, String nom, String prenom, int statut, String mail, String login, String mdp, int adrNo, String adrRue, String adrCP, String adrVille, int grade) { + + public Pompier(int idCaserne, String nom, String prenom, int statut, String mail, String login, String mdp, int adrNo, String adrRue, String adrCP, String adrVille, int grade, String commentaire) { this.idCaserne = idCaserne; this.nom = nom; this.prenom = prenom; @@ -44,9 +45,9 @@ public class Pompier { this.adrCP = adrCP; this.adrVille = adrVille; this.grade = grade; + this.commentaire = commentaire; } - public int getId() { return id; } @@ -151,9 +152,17 @@ public class Pompier { this.grade = grade; } + public String getCommentaire() { + return commentaire; + } + + public void setCommentaire(String commentaire) { + this.commentaire = commentaire; + } + @Override public int hashCode() { - int hash = 7; + int hash = 3; hash = 29 * hash + this.id; hash = 29 * hash + this.idCaserne; hash = 29 * hash + Objects.hashCode(this.nom); @@ -167,6 +176,7 @@ public class Pompier { hash = 29 * hash + Objects.hashCode(this.adrCP); hash = 29 * hash + Objects.hashCode(this.adrVille); hash = 29 * hash + this.grade; + hash = 29 * hash + Objects.hashCode(this.commentaire); return hash; } @@ -221,13 +231,17 @@ public class Pompier { if (!Objects.equals(this.adrVille, other.adrVille)) { return false; } + if (!Objects.equals(this.commentaire, other.commentaire)) { + return false; + } return true; } @Override public String toString() { - return "Pompier{" + "id=" + id + ", idCaserne=" + idCaserne + ", nom=" + nom + ", prenom=" + prenom + ", statut=" + statut + ", mail=" + mail + ", login=" + login + ", mdp=" + mdp + ", adrNo=" + adrNo + ", adrRue=" + adrRue + ", adrCP=" + adrCP + ", adrVille=" + adrVille + ", grade=" + grade + '}'; + return "Pompier{" + "id=" + id + ", idCaserne=" + idCaserne + ", nom=" + nom + ", prenom=" + prenom + ", statut=" + statut + ", mail=" + mail + ", login=" + login + ", mdp=" + mdp + ", adrNo=" + adrNo + ", adrRue=" + adrRue + ", adrCP=" + adrCP + ", adrVille=" + adrVille + ", grade=" + grade + ", commentaire=" + commentaire + '}'; } + } diff --git a/src/java/com/test/forms/AuthentifForm.java b/src/java/com/test/forms/AuthentifForm.java index 3efbf83..bc8170f 100644 --- a/src/java/com/test/forms/AuthentifForm.java +++ b/src/java/com/test/forms/AuthentifForm.java @@ -5,6 +5,7 @@ */ package com.test.forms; +import bdd.PompierMySql; import com.test.beans.Pompier; import com.test.beans.UserP; import jakarta.servlet.http.HttpServletRequest; @@ -40,12 +41,18 @@ public class AuthentifForm { */ public boolean controlerRole(HttpServletRequest request) { - - - + /* Comparaison entre l'utilisateur saisi et un utilisateur de la base de donées */ +// UserP ChefCaserne = new UserP("Michel", "mRANN"); + + PompierMySql pm = new PompierMySql(); + boolean resul = pm.isAuthentified(request); + resultat = resul?"":"Pseudo ou mdp incorrect"; + return resul; + // Mise à jour de l'attribut resultat + + } - } diff --git a/src/java/com/test/forms/NouveauPompForm.java b/src/java/com/test/forms/NouveauPompForm.java index 62619bb..0c96ab2 100644 --- a/src/java/com/test/forms/NouveauPompForm.java +++ b/src/java/com/test/forms/NouveauPompForm.java @@ -46,8 +46,8 @@ public class NouveauPompForm { if (erreur > 0) return -1; // Creation d'un objet de type Client avec les données transmises - String mdpChiffre = MD5.encode(request.getParameter("ztMDP")); - Pompier unPompier = new Pompier (request.getParameter("id"), + + Pompier unPompier = new Pompier(request.getParameter("id"), request.getParameter("idCaserne"), request.getParameter("ztNom"), request.getParameter("ztPrenom"), @@ -59,7 +59,8 @@ public class NouveauPompForm { request.getParameter("ztAdrRue"), request.getParameter("ztAdrCP"), request.getParameter("ztAdrVille"), - request.getParameter("nGrade") + request.getParameter("nGrade"), + request.getParameter("commentaire") ); PompierMySql cm = new PompierMySql(); int id = cm.createRP(unPompier); // Requête préparée diff --git a/src/java/com/test/servlets/ModifPompierServlet.java b/src/java/com/test/servlets/ModifPompierServlet.java index 42d34d8..2b22b5c 100644 --- a/src/java/com/test/servlets/ModifPompierServlet.java +++ b/src/java/com/test/servlets/ModifPompierServlet.java @@ -57,6 +57,8 @@ public class ModifPompierServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + PompierMysql PompierMs = new PompierMySql(); + request.setAttribute("unPompier", PomperMs.readAll()); processRequest(request, response); } diff --git a/src/java/com/test/servlets/authentifServlet.java b/src/java/com/test/servlets/authentifServlet.java index 3203091..6439bf6 100644 --- a/src/java/com/test/servlets/authentifServlet.java +++ b/src/java/com/test/servlets/authentifServlet.java @@ -78,18 +78,18 @@ public class authentifServlet extends HttpServlet { // Création de l'objet leControle de type AuthentifForm AuthentifForm leControle = new AuthentifForm(); // Appel de la méthode controlerRole - boolean isChef = leControle.controlerRole(request); + boolean isAuthentified = leControle.controlerRole(request); // Création de 2 attributs de requête (isChef et leControle) - request.setAttribute("isChef", isChef); + request.setAttribute("isAuthentified", isAuthentified); request.setAttribute("controlForm", leControle); HttpSession maSession = request.getSession(); maSession.setAttribute("isAuthentified", true); // Affichage de la JSP - if(isChef==true){ - getServletContext().getRequestDispatcher("/WEB-INF/ChefCaserneJSP.jsp") + if(isAuthentified==true){ + getServletContext().getRequestDispatcher("/WEB-INF/PompierJSP.jsp") .forward(request, response); }else{ - getServletContext().getRequestDispatcher("/WEB-INF/PompierJSP.jsp") + getServletContext().getRequestDispatcher("/WEB-INF/accueilJSP.jsp") .forward(request, response); } diff --git a/web/WEB-INF/ChefCaserneJSP.jsp b/web/WEB-INF/ChefCaserneJSP.jsp index 9625ebf..effb6d5 100644 --- a/web/WEB-INF/ChefCaserneJSP.jsp +++ b/web/WEB-INF/ChefCaserneJSP.jsp @@ -5,6 +5,7 @@ --%> <%@include file = "jspf/enteteJSPF.jspf" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> @@ -48,7 +49,7 @@ - + diff --git a/web/WEB-INF/ModifPompier.jsp b/web/WEB-INF/ModifPompier.jsp index 6f5c7b9..c69bb00 100644 --- a/web/WEB-INF/ModifPompier.jsp +++ b/web/WEB-INF/ModifPompier.jsp @@ -5,6 +5,7 @@ --%> <%@include file = "jspf/enteteJSPF.jspf" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> @@ -17,44 +18,42 @@ Fiche Pompier
-

+

-

+

-

+

-

+

-

+

-

+

-

+

-

+
-

+

-

+

-

+

-

+

- - - - - - - - - - - - - + + + + + + + + + + + diff --git a/web/WEB-INF/accueilJSP.jsp b/web/WEB-INF/accueilJSP.jsp index db8ade2..79994f7 100644 --- a/web/WEB-INF/accueilJSP.jsp +++ b/web/WEB-INF/accueilJSP.jsp @@ -8,12 +8,7 @@ <%@page import="com.test.forms.AuthentifForm" %> <%@page contentType="text/html" pageEncoding="UTF-8"%> - -

Bienvenue

-${empty param.ztPseudo ? "Veuillez vous authentifier" : "Authentification réussie"} - - - +
Authentification
@@ -22,13 +17,11 @@ ${empty param.ztPseudo ? "Veuillez vous authentifier" : "Authentification réuss
-
- +

${controlForm.getResultat()}

-
-
+
\ No newline at end of file