Authentification
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -19,6 +19,11 @@
|
||||
<version>${jakartaee}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mariadb.jdbc</groupId>
|
||||
<artifactId>mariadb-java-client</artifactId>
|
||||
<version>3.5.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
49
src/main/java/com/test/bdd/ClientJPA.java
Normal file
49
src/main/java/com/test/bdd/ClientJPA.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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 com.test.beans.Client;
|
||||
import jakarta.ejb.*;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
|
||||
|
||||
@Stateless
|
||||
public class ClientJPA {
|
||||
@PersistenceContext(unitName = "bdclientPU") //en lien avec le fichier persistence.xml : <persistence-unit name="bdclientPU" transaction-type="JTA">
|
||||
private EntityManager em;
|
||||
|
||||
// Mise à jour d'un client
|
||||
public int update(Client client) {
|
||||
Client leClient = em.merge(client);
|
||||
em.flush();
|
||||
return leClient.getId();
|
||||
}
|
||||
|
||||
// Lecture d'un client
|
||||
public Client read(int id) {
|
||||
Client leClient = em.find(Client.class, id);
|
||||
return leClient;
|
||||
}
|
||||
|
||||
// Lecture de tous les clients
|
||||
public List<Client> readAll() {
|
||||
List<Client> lesClients = em.createQuery("SELECT c FROM Client c", Client.class).getResultList();
|
||||
return lesClients;
|
||||
}
|
||||
|
||||
// Création d’un client
|
||||
public int create(Client unClient) {
|
||||
em.persist(unClient); // enregistre l’objet en base
|
||||
// Force la synchro avec la base pour récupérer l’ID tout de suite
|
||||
em.flush();
|
||||
return unClient.getId(); // l’ID est rempli automatiquement par JPA
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
/*
|
||||
* 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 com.test.beans.Client;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
public class ClientMysql {
|
||||
// private Connection laConnexion;
|
||||
private Statement stmt = null;
|
||||
private ResultSet result = null;
|
||||
|
||||
// public ClientMysql() {
|
||||
Connection laConnexion = Connexion.getConnect("192.168.100.100", "bdclient", "adminBDClient", "mdpBDClient");
|
||||
// }
|
||||
|
||||
public ArrayList<Client> readAll() {
|
||||
ArrayList<Client> clients = new ArrayList<Client>();
|
||||
|
||||
try {
|
||||
stmt = laConnexion.createStatement();
|
||||
|
||||
result = stmt.executeQuery("SELECT * FROM client");
|
||||
while (result.next()) {
|
||||
Client client = new Client(
|
||||
result.getInt("id"),
|
||||
result.getString("nom"),
|
||||
result.getString("prenom"),
|
||||
result.getString("mail")
|
||||
);
|
||||
|
||||
clients.add(client);
|
||||
}
|
||||
|
||||
result.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 clients;
|
||||
}
|
||||
|
||||
public int create(Client unClient){
|
||||
int id=-1;
|
||||
String sql = "INSERT INTO client (nom, prenom, mail) VALUES (?,?,?);";
|
||||
|
||||
try (PreparedStatement prepStmt = laConnexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
|
||||
prepStmt.setString(1, unClient.getNom());//le param. 1 est remplacé par le nom du client
|
||||
prepStmt.setString(2, unClient.getPrenom());
|
||||
prepStmt.setString(3, unClient.getMail());
|
||||
|
||||
int status = prepStmt.executeUpdate();
|
||||
// récupération de la clé auto-générée
|
||||
if (status > 0) {
|
||||
try (ResultSet result = prepStmt.getGeneratedKeys()) {
|
||||
if (result.next()) { // <-- utiliser next() et pas first()
|
||||
id = result.getInt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
prepStmt.close();
|
||||
|
||||
} catch(SQLException ex){
|
||||
System.out.println("SQLException:"+ex.getMessage());
|
||||
System.out.println("SQLState:"+ex.getSQLState());
|
||||
System.out.println("code Erreur:"+ex.getErrorCode());
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
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 Connexion {
|
||||
private static Connection connect; // Variable de connexion
|
||||
|
||||
/**
|
||||
* Constructeur
|
||||
* @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 Connexion(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 accessible");
|
||||
|
||||
// 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
|
||||
url += "?autoReconnect=true"; // Ajout 26/09/2021
|
||||
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) {
|
||||
System.out.println("getConnect");
|
||||
if (connect == null) {
|
||||
new Connexion(serveur, bdd, nomUtil, mdp);
|
||||
}
|
||||
return connect;
|
||||
}
|
||||
}
|
||||
@@ -32,9 +32,10 @@ public class SalarieJPA {
|
||||
}
|
||||
|
||||
public Salarie readSalarie(String login, String mdp) {
|
||||
TypedQuery<Salarie> query = em.createQuery("SELECT s FROM Salarie s WHERE s.login = ?1 AND s.mdp= ?2 LIMIT 1", Salarie.class);
|
||||
TypedQuery<Salarie> query = em.createQuery("SELECT s FROM Salarie s WHERE s.login = ?1 AND s.mdp= ?2", Salarie.class);
|
||||
Salarie salarie = query.setParameter(1, login)
|
||||
.setParameter(2, mdp)
|
||||
.setMaxResults(1)
|
||||
.getSingleResult();
|
||||
|
||||
return salarie;
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* 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 com.test.beans.Salarie;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
public class SalarieMysql {
|
||||
// private Connection laConnexion;
|
||||
private Statement stmt = null;
|
||||
private ResultSet result = null;
|
||||
|
||||
// public ClientMysql() {
|
||||
Connection laConnexion = Connexion.getConnect("192.168.100.100", "bdclient", "adminBDClient", "mdpBDClient");
|
||||
// }
|
||||
|
||||
public ArrayList<Salarie> readAll() {
|
||||
ArrayList<Salarie> salaries = new ArrayList<Salarie>();
|
||||
|
||||
try {
|
||||
stmt = laConnexion.createStatement();
|
||||
|
||||
result = stmt.executeQuery("SELECT * FROM salarie");
|
||||
while (result.next()) {
|
||||
Salarie salarie = new Salarie(
|
||||
result.getInt("id"),
|
||||
result.getString("login"),
|
||||
result.getString("mdp"),
|
||||
result.getInt("admin")
|
||||
);
|
||||
|
||||
salaries.add(salarie);
|
||||
}
|
||||
|
||||
result.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 salaries;
|
||||
}
|
||||
|
||||
public Salarie lireSalarie(String login, String mdp) {
|
||||
Salarie salarie = null;
|
||||
|
||||
try {
|
||||
PreparedStatement preparedStmt = laConnexion.prepareStatement("SELECT * FROM salarie WHERE login = ? AND mdp = ?");
|
||||
|
||||
preparedStmt.setString(1, login);
|
||||
preparedStmt.setString(2, mdp);
|
||||
|
||||
result = preparedStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
salarie = new Salarie(
|
||||
result.getInt("id"),
|
||||
result.getString("login"),
|
||||
result.getString("mdp"),
|
||||
result.getInt("admin")
|
||||
);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.out.println("SQLException : " + ex.getMessage());
|
||||
System.out.println("SQLState : " + ex.getSQLState());
|
||||
System.out.println("Code erreur : " + ex.getErrorCode());
|
||||
}
|
||||
|
||||
return salarie;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,61 +3,54 @@
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.test.beans;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name = "client")
|
||||
|
||||
public class Client {
|
||||
|
||||
// Annotations pour gérer id auto-incrémenté
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
private String nom;
|
||||
private String prenom;
|
||||
private String mail;
|
||||
|
||||
public Client() {} // obligatoire pour JPA (constructeur vide)
|
||||
|
||||
public Client(int id, String nom, String prenom, String mail) {
|
||||
this.id=id;
|
||||
this.nom = nom;
|
||||
this.prenom = prenom;
|
||||
this.mail = mail;
|
||||
}
|
||||
|
||||
public Client(String nom, String prenom, String mail) {
|
||||
this.nom = nom;
|
||||
this.prenom = prenom;
|
||||
this.mail = mail;
|
||||
}
|
||||
|
||||
public Client(int id, String nom, String prenom, String mail) {
|
||||
this.id = id;
|
||||
this.nom = nom;
|
||||
this.prenom = prenom;
|
||||
this.mail = mail;
|
||||
// Getters, setters, toString() identiques
|
||||
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 + '}';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,53 +3,52 @@
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.test.beans;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name = "salarie")
|
||||
|
||||
public class Salarie {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private int id;
|
||||
|
||||
private String login;
|
||||
private String mdp;
|
||||
private boolean admin;
|
||||
private int admin;
|
||||
|
||||
public Salarie() {} // obligatoire pour JPA (constructeur vide)
|
||||
|
||||
public Salarie(int id, String login, String mdp, int admin) {
|
||||
this.id = id;
|
||||
this.login = login;
|
||||
this.mdp = mdp;
|
||||
this.admin = admin > 0;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getMdp() {
|
||||
return mdp;
|
||||
}
|
||||
|
||||
public void setMdp(String mdp) {
|
||||
this.mdp = mdp;
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
return admin;
|
||||
}
|
||||
|
||||
public void setAdmin(boolean admin) {
|
||||
this.admin = admin;
|
||||
}
|
||||
|
||||
public Salarie(String login, String mdp, int admin) {
|
||||
this.login = login;
|
||||
this.mdp = mdp;
|
||||
this.admin = admin;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
public void setId(int id) { this.id = id; }
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
public String getMdp() { return mdp; }
|
||||
public void setMdp(String mdp) { this.mdp = mdp; }
|
||||
public int getAdmin() { return admin; };
|
||||
public void setAdmin(int admin) { this.admin = admin; };
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Salarie{" + "id=" + id + ", login=" + login + ", mdp=" + mdp + ", admin=" + admin + '}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,20 +4,21 @@
|
||||
*/
|
||||
package com.test.forms;
|
||||
|
||||
import com.test.bdd.SalarieMysql;
|
||||
import com.test.bdd.SalarieJPA;
|
||||
import com.test.beans.Salarie;
|
||||
import com.test.beans.User;
|
||||
import jakarta.ejb.*;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
@Stateless
|
||||
public class AuthentifForm {
|
||||
@EJB
|
||||
private SalarieJPA dao;
|
||||
public String resultat;
|
||||
|
||||
public AuthentifForm() {};
|
||||
|
||||
public String getResultat() {
|
||||
return this.resultat;
|
||||
}
|
||||
@@ -27,10 +28,9 @@ public class AuthentifForm {
|
||||
}
|
||||
|
||||
public boolean existeUser(HttpServletRequest request) {
|
||||
SalarieMysql salarieDB = new SalarieMysql();
|
||||
String pseudo = (String) request.getParameter("pseudo");
|
||||
String mdp = (String) request.getParameter("mdp");
|
||||
Salarie user = salarieDB.lireSalarie(pseudo, mdp);
|
||||
Salarie user = dao.readSalarie(pseudo, mdp);
|
||||
|
||||
boolean existe = user != null;
|
||||
|
||||
@@ -42,11 +42,10 @@ public class AuthentifForm {
|
||||
}
|
||||
|
||||
public boolean controlerAdmin(HttpServletRequest request) {
|
||||
SalarieMysql salarieDB = new SalarieMysql();
|
||||
String pseudo = (String) request.getParameter("pseudo");
|
||||
String mdp = (String) request.getParameter("mdp");
|
||||
Salarie user = salarieDB.lireSalarie(pseudo, mdp);
|
||||
boolean estAdmin = user.isAdmin();
|
||||
Salarie user = dao.readSalarie(pseudo, mdp);
|
||||
boolean estAdmin = user.getAdmin() > 0;
|
||||
|
||||
if (estAdmin) {
|
||||
this.setResultat("Vous êtes administrateur");
|
||||
|
||||
@@ -4,18 +4,22 @@
|
||||
*/
|
||||
package com.test.forms;
|
||||
|
||||
import com.test.bdd.ClientMysql;
|
||||
import com.test.bdd.ClientJPA;
|
||||
import com.test.beans.Client;
|
||||
import jakarta.ejb.EJB;
|
||||
import jakarta.ejb.Stateless;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
@Stateless
|
||||
public class ModifClientForm {
|
||||
@EJB
|
||||
private ClientJPA dao;
|
||||
public String resultat;
|
||||
|
||||
public ModifClientForm() {};
|
||||
|
||||
public String getResultat() {
|
||||
return resultat;
|
||||
}
|
||||
@@ -25,13 +29,28 @@ public class ModifClientForm {
|
||||
}
|
||||
|
||||
public boolean modifierClient(HttpServletRequest request) {
|
||||
ClientMysql clientDB = new ClientMysql();
|
||||
boolean modification = false;
|
||||
String id = request.getParameter("clientId");
|
||||
String nom = request.getParameter("clientNom");
|
||||
String prenom = request.getParameter("clientPrenom");
|
||||
String mail = request.getParameter("clientMail");
|
||||
Client client = dao.read(Integer.parseInt(id));
|
||||
|
||||
boolean modification = clientDB.update(Integer.parseInt(id), nom, prenom, mail);
|
||||
if (client != null) {
|
||||
if (nom != null && !nom.isEmpty()) {
|
||||
client.setNom(nom);
|
||||
}
|
||||
|
||||
if (prenom != null && !prenom.isEmpty()) {
|
||||
client.setPrenom(prenom);
|
||||
}
|
||||
|
||||
if (mail != null && !mail.isEmpty()) {
|
||||
client.setMail(mail);
|
||||
}
|
||||
|
||||
modification = dao.update(client) == Integer.parseInt(id);
|
||||
}
|
||||
|
||||
if (modification) {
|
||||
this.setResultat("Le client N°"+id+" a été modifié avec succès");
|
||||
|
||||
@@ -4,18 +4,22 @@
|
||||
*/
|
||||
package com.test.forms;
|
||||
|
||||
import com.test.bdd.ClientMysql;
|
||||
import com.test.bdd.ClientJPA;
|
||||
import com.test.beans.Client;
|
||||
import jakarta.ejb.*;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author steve.maingana
|
||||
*/
|
||||
public class NouveauClientForm {
|
||||
public String resultat;
|
||||
|
||||
public NouveauClientForm() {};
|
||||
@Stateless
|
||||
public class NouveauClientForm {
|
||||
@EJB
|
||||
private ClientJPA dao;
|
||||
|
||||
public String resultat;
|
||||
|
||||
public String getResultat() {
|
||||
return resultat;
|
||||
@@ -26,9 +30,16 @@ public class NouveauClientForm {
|
||||
}
|
||||
|
||||
public int verifierClient(HttpServletRequest request) {
|
||||
Client client = new Client(request.getParameter("nom_client"), request.getParameter("prenom_client"), request.getParameter("mail_client"));
|
||||
ClientMysql clientDB = new ClientMysql();
|
||||
int nouveauClient = clientDB.create(client);
|
||||
return nouveauClient;
|
||||
int id=-1;
|
||||
|
||||
String leNom = request.getParameter("nom");
|
||||
String lePrenom =request.getParameter("prenom");
|
||||
|
||||
if ((lePrenom.matches("[A-Za-zÀ-ÖØ-öø-ÿ' -]{1,100}"))&& (leNom.matches("[A-Za-zÀ-ÖØ-öø-ÿ' -]{1,100}"))){
|
||||
Client cliSaisi = new Client(request.getParameter("nom"),request.getParameter("prenom"),request.getParameter("mail") );
|
||||
id = dao.create(cliSaisi);
|
||||
System.out.println("Client créé avec id : " + id);
|
||||
}
|
||||
return id;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package com.test.servlets;
|
||||
|
||||
import com.test.forms.AuthentifForm;
|
||||
import jakarta.ejb.EJB;
|
||||
import jakarta.servlet.RequestDispatcher;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
@@ -23,9 +24,8 @@ import jakarta.servlet.http.HttpSession;
|
||||
@WebServlet(name = "AuthentifServlet", urlPatterns = {"/authentification"})
|
||||
|
||||
public class AuthentifServlet extends HttpServlet {
|
||||
// String monPseudo = "Lovelace";
|
||||
String monPseudo = "steve";
|
||||
String monMdp = "Ada";
|
||||
@EJB
|
||||
private AuthentifForm auhtentifForm;
|
||||
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
|
||||
@@ -82,18 +82,17 @@ public class AuthentifServlet extends HttpServlet {
|
||||
HttpSession maSession = request.getSession();
|
||||
String pseudo = (String) request.getParameter("pseudo");
|
||||
String mdp = (String) request.getParameter("mdp");
|
||||
AuthentifForm authentification = new AuthentifForm();
|
||||
maSession.setAttribute("isAuthentified", false);
|
||||
|
||||
if (!pseudo.isBlank() && !mdp.isBlank()) {
|
||||
if (authentification.existeUser(request)) {
|
||||
boolean isAdmin = authentification.controlerAdmin(request);
|
||||
if (auhtentifForm.existeUser(request)) {
|
||||
boolean isAdmin = auhtentifForm.controlerAdmin(request);
|
||||
maSession.setAttribute("isAdmin", isAdmin);
|
||||
maSession.setAttribute("isAuthentified", true);
|
||||
}
|
||||
}
|
||||
|
||||
request.setAttribute("authentification", authentification);
|
||||
request.setAttribute("authentification", auhtentifForm);
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/accueilVue.jsp");
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
*/
|
||||
package com.test.servlets;
|
||||
|
||||
import com.test.bdd.ClientMysql;
|
||||
import com.test.bdd.ClientJPA;
|
||||
import com.test.beans.Client;
|
||||
import jakarta.ejb.EJB;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -15,6 +16,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -24,6 +26,8 @@ import java.util.ArrayList;
|
||||
@WebServlet(name = "ListeServlet", urlPatterns = {"/ListeClients"})
|
||||
|
||||
public class ListeServlet extends HttpServlet {
|
||||
@EJB
|
||||
private ClientJPA dao;
|
||||
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
|
||||
@@ -69,11 +73,9 @@ public class ListeServlet extends HttpServlet {
|
||||
response.sendRedirect("/Test2Jakarta/Accueil");
|
||||
return;
|
||||
}
|
||||
ClientMysql clientDB = new ClientMysql();
|
||||
ArrayList<Client> clients = clientDB.readAll();
|
||||
|
||||
List<Client> clients = dao.readAll();
|
||||
request.setAttribute("clients", clients);
|
||||
|
||||
getServletContext().getRequestDispatcher("/WEB-INF/listeClientsVue.jsp").forward(request, response);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package com.test.servlets;
|
||||
|
||||
import com.test.forms.ModifClientForm;
|
||||
import jakarta.ejb.EJB;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -20,6 +21,8 @@ import jakarta.servlet.http.HttpSession;
|
||||
*/
|
||||
@WebServlet(name = "ModifClientServlet", urlPatterns = {"modifierClient"})
|
||||
public class ModifClientServlet extends HttpServlet {
|
||||
@EJB
|
||||
private ModifClientForm modifClient;
|
||||
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
|
||||
@@ -81,10 +84,9 @@ public class ModifClientServlet extends HttpServlet {
|
||||
return;
|
||||
}
|
||||
|
||||
ModifClientForm modification = new ModifClientForm();
|
||||
boolean modifClient = modification.modifierClient(request);
|
||||
modifClient.modifierClient(request);
|
||||
request.setAttribute("modification", modifClient);
|
||||
request.setAttribute("modification_message", modification.getResultat());
|
||||
request.setAttribute("modification_message", modifClient.getResultat());
|
||||
|
||||
response.sendRedirect("/Test2Jakarta/ListeClients");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package com.test.servlets;
|
||||
|
||||
import com.test.forms.NouveauClientForm;
|
||||
import jakarta.ejb.EJB;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -23,6 +24,8 @@ import java.util.ArrayList;
|
||||
@WebServlet(name = "NouveauServlet", urlPatterns = {"/NouveauClient"})
|
||||
|
||||
public class NouveauServlet extends HttpServlet {
|
||||
@EJB
|
||||
private NouveauClientForm leControle;
|
||||
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
|
||||
@@ -85,9 +88,7 @@ public class NouveauServlet extends HttpServlet {
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
NouveauClientForm nouveauClient = new NouveauClientForm();
|
||||
int idClient = nouveauClient.verifierClient(request);
|
||||
int idClient = leControle.verifierClient(request);
|
||||
request.setAttribute("new_client", idClient > -1);
|
||||
|
||||
if (idClient > -1) {
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">
|
||||
<!-- Define Persistence Unit -->
|
||||
<persistence-unit name="my_persistence_unit">
|
||||
|
||||
<!-- Define Persistence Unit -->
|
||||
<persistence-unit name="bdclientPU" transaction-type="JTA">
|
||||
<jta-data-source>jdbc/bdclient</jta-data-source>
|
||||
<properties>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:mariadb://192.168.100.100/bdclient"/>
|
||||
<property name="jakarta.persistence.jdbc.user" value="adminBDClient"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="mdpBDClient"/>
|
||||
<!-- Optionnel : génération du schéma -->
|
||||
<property name="jakarta.persistence.schema-generation.database.action" value="none"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
|
||||
@@ -16,11 +16,11 @@
|
||||
<fieldset>
|
||||
<legend>Client :</legend>
|
||||
<label>Nom :</label>
|
||||
<input type="text" name="nom_client">
|
||||
<input type="text" name="nom">
|
||||
<label>Prénom :</label>
|
||||
<input type="text" name="prenom_client">
|
||||
<input type="text" name="prenom">
|
||||
<label>Adresse mail :</label>
|
||||
<input type="email" name="mail_client">
|
||||
<input type="email" name="mail">
|
||||
<input type="submit" value="Compléter">
|
||||
</fieldset>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user