diff --git a/src/main/java/com/test/bdd/ClientMysql.java b/src/main/java/com/test/bdd/ClientMysql.java new file mode 100644 index 0000000..8c150d0 --- /dev/null +++ b/src/main/java/com/test/bdd/ClientMysql.java @@ -0,0 +1,110 @@ +/* + * 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.bdd; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import com.test.beans.Client; +import java.sql.PreparedStatement; + +/** + * + * @author emile.malcuit + */ +public class ClientMysql { + private Connection laConnexion = connexionSQL.getConnect("192.168.100.100", "bdclient", "adminClient", "admin"); + + public ClientMysql() { + } + + public Connection getLaConnexion() { + return laConnexion; + } + + + + public void createClient(Client newCli) { + String commande = "INSERT INTO client(nom, prenom, mail) VALUES (?, ?, ?)"; + + try (PreparedStatement pstmt = laConnexion.prepareStatement(commande)) { + pstmt.setString(1, newCli.getNom()); + pstmt.setString(2, newCli.getPrenom()); + pstmt.setString(3, newCli.getMail()); + + int rowsInserted = pstmt.executeUpdate(); + + if (rowsInserted > 0) { + System.out.println("Client inséré avec succès !"); + } + } catch (SQLException ex) { + System.err.println("Erreur lors de l'insertion du client : " + ex.getMessage()); + } + } + + + + public ArrayList readAll(){ + ArrayList lesClients=new ArrayList<>(); + try { + Statement stmt=laConnexion.createStatement(); + ResultSet resultQ=null; + resultQ=stmt.executeQuery("SELECT * FROM client;"); + while (resultQ.next()){ + Client resultat=new Client(resultQ.getInt("id"),resultQ.getString("nom"),resultQ.getString("prenom"),resultQ.getString("mail")); + lesClients.add(resultat); + } + resultQ.close(); + stmt.close(); + + } + catch(SQLException ex){ + System.out.println("SQLException:"+ex.getMessage()); + System.out.println("SQLState:"+ex.getSQLState()); + System.out.println("code Erreur:"+ex.getErrorCode()); + } + return lesClients; + } + + + public Client read(int id){ + String commande = "SELECT * FROM client WHERE id = ?"; + Client recherche = new Client(); + + try(PreparedStatement pstmt = laConnexion.prepareStatement(commande)){ + pstmt.setString(1, Integer.toString(id)); + ResultSet resultQ = pstmt.executeQuery(); + recherche.setId(resultQ.getInt("id")); + recherche.setNom(resultQ.getString("nom")); + recherche.setPrenom(resultQ.getString("prenom")); + recherche.setMail(resultQ.getString("mail")); + + } catch (SQLException ex) { + System.err.println("Erreur lors de la recherche du client : " + ex.getMessage()); + } + + return recherche; + } + + + public void delete(int id){ + String commande = "DELETE FROM client WHERE id = ?"; + + try(PreparedStatement pstmt = laConnexion.prepareStatement(commande)){ + pstmt.setString(1, Integer.toString(id)); + ResultSet resultQ = pstmt.executeQuery(); + + }catch(SQLException ex){ + System.err.println("Erreur lors de la suppression du client : " + ex.getMessage()); + } + } + + +} + + + diff --git a/src/main/java/com/test/bdd/connexionSQL.java b/src/main/java/com/test/bdd/connexionSQL.java new file mode 100644 index 0000000..e90d34d --- /dev/null +++ b/src/main/java/com/test/bdd/connexionSQL.java @@ -0,0 +1,61 @@ +package com.test.bdd; + +/* +Connexion.java +Classe permettant d'établir une connexion avec une base de données mySQL +*/ +import java.sql.Connection; +import java.sql.DriverManager; + +public class connexionSQL { + private static Connection connect; // Variable de connexion + + /** + * Constructeur d'une connexion avec une base de données + * @param serveur nom du serveur, localhost si local + * @param bdd nom de la base de données + * @param nomUtil nom utilisateur + * @param mdp mot de passe lié à l'utilisateur + */ + private connexionSQL(String serveur, String bdd, String nomUtil, String mdp) { + try { + // 1. Chargement du driver + //Class.forName("com.mysql.jdbc.Driver"); + Class.forName("org.mariadb.jdbc.Driver"); + System.out.println("Driver MariaDb Ok"); + + // 2. Initialisation des paramètres de connexion + String host = serveur; // Serveur de bd + String dbname = bdd; // Nom bd + String url = "jdbc:mariadb://" + host + "/" + dbname; // url de connexion + System.out.println("url : "+url); + String user = nomUtil; // nom du user + System.out.println("nomUtil : "+nomUtil); + String passwd = mdp; // mot de passe + System.out.println("mdp : "+mdp); + + // 3. Connexion + connect = (Connection) DriverManager.getConnection(url, user, passwd); + System.out.println("Connexion réussie !"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Retourne la connection �tablie (Cr�ation d'une connection si elle n'existe pas) + * @param serveur nom du serveur, localhost si local + * @param bdd nom de la base de donn�es + * @param nomUtil nom utilisateur + * @param mdp mot de passe li� � l'utilisateur + * @return connection �tablie + */ + public static Connection getConnect(String serveur, String bdd, String nomUtil, String mdp) { + if (connect == null) { + new connexionSQL(serveur, bdd, nomUtil, mdp); + } + System.out.println("Connexion validé"); + return connect; + } +} diff --git a/src/main/java/com/test/beans/Client.java b/src/main/java/com/test/beans/Client.java new file mode 100644 index 0000000..2d8469d --- /dev/null +++ b/src/main/java/com/test/beans/Client.java @@ -0,0 +1,72 @@ +/* + * 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.beans; + +/** + * + * @author emile.malcuit + */ +public class Client { + private int id; + private String nom; + private String prenom; + private String mail; + + + public Client() { + } + + public Client(int id, String nom, String prenom, String mail) { + this.id = id; + this.nom = nom; + this.prenom = prenom; + this.mail = mail; + } + + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getNom() { + return nom; + } + + public void setNom(String nom) { + this.nom = nom; + } + + public String getPrenom() { + return prenom; + } + + public void setPrenom(String prenom) { + this.prenom = prenom; + } + + public String getMail() { + return mail; + } + + public void setMail(String mail) { + this.mail = mail; + } + + @Override + public String toString() { + return "Client{" + "id=" + id + ", nom=" + nom + ", prenom=" + prenom + ", mail=" + mail + '}'; + } + + + + + + +} diff --git a/src/main/java/com/test/beans/User.java b/src/main/java/com/test/beans/User.java new file mode 100644 index 0000000..97706d4 --- /dev/null +++ b/src/main/java/com/test/beans/User.java @@ -0,0 +1,62 @@ +/* + * 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.beans; + +import java.util.Objects; + +/** + * + * @author emile.malcuit + */ +public class User { + private String pseudo; + private String motDePasse; + + + + public User(String pseudo, String motDePasse) { + this.pseudo = pseudo; + this.motDePasse = motDePasse; + } + + + public String getPseudo() { + return pseudo; + } + + public void setPseudo(String pseudo) { + this.pseudo = pseudo; + } + + public String getMotDePasse() { + return motDePasse; + } + + public void setMotDePasse(String motDePasse) { + this.motDePasse = motDePasse; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final User other = (User) obj; + if (!Objects.equals(this.pseudo, other.pseudo)) { + return false; + } + return Objects.equals(this.motDePasse, other.motDePasse); + } + + + + +} diff --git a/src/main/java/com/test/forms/AuthentifForm.java b/src/main/java/com/test/forms/AuthentifForm.java new file mode 100644 index 0000000..198cae1 --- /dev/null +++ b/src/main/java/com/test/forms/AuthentifForm.java @@ -0,0 +1,45 @@ +/* + * 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.beans.User; +import jakarta.servlet.http.HttpServletRequest; + +/** + * + * @author emile.malcuit + */ +public class AuthentifForm { + private String resultat = "Vous n'êtes pas administrateur"; + + public String getResultat() { + return resultat; + } + + public void setResultat(String resultat) { + this.resultat = resultat; + } + + + public boolean controlerAdmin(HttpServletRequest request){ + User admin = new User("Lovelace", "Ada"); + String login = request.getParameter("user_name"); + String mdp = request.getParameter("user_password"); + User userSaisi = new User(login, mdp); + boolean retour = false; + if(userSaisi.equals(admin)){ + retour = true; + setResultat("Vous êtes administrateur"); + } + return retour; + + + } + + + + + +} diff --git a/src/main/java/com/test/servlets/AuthentifServlet.java b/src/main/java/com/test/servlets/AuthentifServlet.java new file mode 100644 index 0000000..c14d328 --- /dev/null +++ b/src/main/java/com/test/servlets/AuthentifServlet.java @@ -0,0 +1,98 @@ +/* + * 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.AuthentifForm; +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; + +/** + * + * @author emile.malcuit + */ +@WebServlet(name="AuthentifServlet", urlPatterns={"/Authentification"}) +public class AuthentifServlet 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 AuthentifServlet"); + out.println(""); + out.println(""); + out.println("

Servlet AuthentifServlet 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 { + //processRequest(request, response); + + AuthentifForm authent = new AuthentifForm(); + boolean isAdmin = authent.controlerAdmin(request); + String status = authent.getResultat(); + + request.setAttribute("isAdmin", isAdmin); + request.setAttribute("status", status); + request.setAttribute("pseudo", request.getAttribute("user_name")); + + getServletContext().getRequestDispatcher("/WEB-INF/AcceuilJSP.jsp").forward(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/ListeServlet.java b/src/main/java/com/test/servlets/ListeServlet.java index bb0773b..b5213d5 100644 --- a/src/main/java/com/test/servlets/ListeServlet.java +++ b/src/main/java/com/test/servlets/ListeServlet.java @@ -4,6 +4,8 @@ */ package com.test.servlets; +import com.test.bdd.ClientMysql; +import com.test.beans.Client; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletException; @@ -11,6 +13,7 @@ import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.ArrayList; /** * @@ -58,6 +61,10 @@ public class ListeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); + ClientMysql UnClient = new ClientMysql(); + ArrayList lesClients = UnClient.readAll(); + + request.setAttribute("lesClients", lesClients); getServletContext().getRequestDispatcher("/WEB-INF/ListeClientsJSP.jsp").forward(request, response); } diff --git a/src/main/java/com/test/servlets/NouveauServlet.java b/src/main/java/com/test/servlets/NouveauServlet.java index 389bb58..0c08c55 100644 --- a/src/main/java/com/test/servlets/NouveauServlet.java +++ b/src/main/java/com/test/servlets/NouveauServlet.java @@ -4,6 +4,8 @@ */ package com.test.servlets; +import com.test.bdd.ClientMysql; +import com.test.beans.Client; import java.io.IOException; import java.io.PrintWriter; import jakarta.servlet.ServletException; @@ -73,7 +75,19 @@ public class NouveauServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - processRequest(request, response); + + //Recuperation des informations inserer dans le formulaire nouveau client + Client newCli = new Client(); + newCli.setNom(request.getParameter("nom")); + newCli.setPrenom(request.getParameter("prenom")); + newCli.setMail(request.getParameter("email")); + + //Insertion du client en db + ClientMysql cliSQL = new ClientMysql(); + cliSQL.createClient(newCli); + + response.sendRedirect(request.getContextPath() + "/ListeClients"); + } /** diff --git a/src/main/java/com/test/servlets/TestClientSQL.java b/src/main/java/com/test/servlets/TestClientSQL.java new file mode 100644 index 0000000..d17452b --- /dev/null +++ b/src/main/java/com/test/servlets/TestClientSQL.java @@ -0,0 +1,95 @@ +package com.test.servlets; + +/* + * 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 + */ + +import com.test.bdd.ClientMysql; +import com.test.beans.Client; +import java.io.IOException; +import java.io.PrintWriter; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.ArrayList; + +/** + * + * @author emile.malcuit + */ +public class TestClientSQL 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 TestClientSQL"); + out.println(""); + out.println(""); + ClientMysql UnClient = new ClientMysql(); + ArrayList lesClients = UnClient.readAll(); + for(Client unClient:lesClients){ + out.println(unClient.toString()); + } + out.println("

Servlet TestClientSQL 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 { + 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/webapp/WEB-INF/AcceuilJSP.jsp b/src/main/webapp/WEB-INF/AcceuilJSP.jsp index caa6dae..a485dd8 100644 --- a/src/main/webapp/WEB-INF/AcceuilJSP.jsp +++ b/src/main/webapp/WEB-INF/AcceuilJSP.jsp @@ -13,33 +13,29 @@ - <%@include file="jspf/entete.jspf" %> - - - - - - <% - - Object connect = request.getAttribute("connect"); - Object login = request.getAttribute("login"); - if(connect.equals("true")){ - - %> - <%@include file="jspf/menu.jspf" %> -

Test ${connect}

-

Hello mon ami ${login}

-

Bienvenue sur la nouvelle application de gestion des clients - écrite avec JakartaEE

- <% - }else{ - %> - -
+ <%@include file="jspf/entete.jspf" %> + + +<% + Boolean isAdmin = (Boolean) request.getAttribute("isAdmin"); + String status = (String) request.getAttribute("status"); + String login = (String) request.getAttribute("pseudo"); + + if (isAdmin != null && isAdmin) { +%> + <%@include file="jspf/menu.jspf" %> +

Test ${isAdmin}

+

Hello mon ami ${login}

+

${status}

+<% + } else if (isAdmin != null && !isAdmin) { +%> +

Hello ${login}

+

${status}

+<% + } else { +%> +

Formulaire de connexion

@@ -61,9 +57,9 @@
- <% - } - %> - - +<% + } +%> + + diff --git a/src/main/webapp/WEB-INF/ListeClientsJSP.jsp b/src/main/webapp/WEB-INF/ListeClientsJSP.jsp index 8973325..10a86f2 100644 --- a/src/main/webapp/WEB-INF/ListeClientsJSP.jsp +++ b/src/main/webapp/WEB-INF/ListeClientsJSP.jsp @@ -5,6 +5,7 @@ --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> @@ -13,11 +14,31 @@ + <% + %> <%@include file="jspf/entete.jspf" %> <%@include file="jspf/menu.jspf" %> +

Hello World!

-

Liste des clients +

Liste des clients


- Page en construction

+ + + + + + + + + + + + + + + + +
IDNomPrénomMail
${client.id}${client.nom}${client.prenom}${client.mail}
+ diff --git a/src/main/webapp/WEB-INF/NouveauClientJSP.jsp b/src/main/webapp/WEB-INF/NouveauClientJSP.jsp index 23db03d..222019f 100644 --- a/src/main/webapp/WEB-INF/NouveauClientJSP.jsp +++ b/src/main/webapp/WEB-INF/NouveauClientJSP.jsp @@ -16,8 +16,22 @@ <%@include file="jspf/entete.jspf" %> <%@include file="jspf/menu.jspf" %>

Bonjour Les Zouaves

-

Création d’un nouveau client +

Création d’un nouveau client


- Page en construction

+

Insertion de client

+
+
+

+ +
+

+ +
+

+ +
+ +
+
diff --git a/src/main/webapp/WEB-INF/jspf/menu.jspf b/src/main/webapp/WEB-INF/jspf/menu.jspf index b2bda20..ec5f64d 100644 --- a/src/main/webapp/WEB-INF/jspf/menu.jspf +++ b/src/main/webapp/WEB-INF/jspf/menu.jspf @@ -1,7 +1,7 @@ <%-- any content can be specified here e.g.: --%> <%@ page pageEncoding="UTF-8" %> diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 02193b0..41d657d 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -12,6 +12,14 @@ ListeServlet com.test.servlets.ListeServlet + + AuthentifServlet + com.test.servlets.AuthentifServlet + + + TestClientSQL + com.test.servlets.TestClientSQL + AcceuilServlet /Acceuil @@ -24,6 +32,14 @@ ListeServlet /ListeClients + + AuthentifServlet + /Authentification + + + TestClientSQL + /TestClientSQL + 30 diff --git a/src/main/webapp/style/style.css b/src/main/webapp/style/style.css index eb1f58a..538be16 100644 --- a/src/main/webapp/style/style.css +++ b/src/main/webapp/style/style.css @@ -1,6 +1,6 @@ /* Style de la navigation */ nav { - background-color: #333; /* Couleur de fond sombre pour la navigation */ + background-color: #007bff; /* Couleur de fond sombre pour la navigation */ padding: 10px 20px; /* Espacement intérieur */ text-align: center; /* Centrer les éléments */ } @@ -108,3 +108,29 @@ button[type="submit"] { button[type="submit"]:hover { background-color: #0056b3; } + +table { + border-collapse: collapse; + width: 80%; + margin: 20px auto; + font-family: Arial, sans-serif; + } + + th, td { + border: 1px solid #ccc; + padding: 10px 15px; + text-align: left; + } + + th { + background-color: #2c3e50; + color: white; + } + + tr:nth-child(even) { + background-color: #f2f2f2; + } + + tr:hover { + background-color: #dcdde1; + } diff --git a/src/test/java/testClient.java b/src/test/java/testClient.java new file mode 100644 index 0000000..94c344f --- /dev/null +++ b/src/test/java/testClient.java @@ -0,0 +1,12 @@ +/* + * 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 + */ + +/** + * + * @author emile.malcuit + */ +public class testClient { + +}