tp 3 / tp 4

This commit is contained in:
2025-09-22 17:52:24 +02:00
parent e16cd32c4b
commit eae28235bc
14 changed files with 438 additions and 22 deletions

View File

@@ -0,0 +1,55 @@
/*
* 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.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;
}
}

View File

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

View File

@@ -0,0 +1,57 @@
/*
* 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 steve.maingana
*/
public class Client {
private int id;
private String nom;
private String prenom;
private String mail;
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;
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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 steve.maingana
*/
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 this.pseudo;
}
private String getMDP() {
return this.motDePasse;
}
public void setPseudo(String pseudo) {
this.pseudo = pseudo;
}
public void setMDP(String MDP) {
this.motDePasse = MDP;
}
@Override
public boolean equals(Object user) {
boolean resultat = false;
if (user.getClass().equals(this.getClass())) {
User toCompare = (User) user;
if (this.getPseudo().equals(toCompare.getPseudo()) && this.getMDP().equals(toCompare.getMDP())) {
resultat = true;
}
}
return resultat;
};
}

View File

@@ -0,0 +1,40 @@
/*
* 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 steve.maingana
*/
public class AuthentifForm {
public String resultat;
public AuthentifForm() {};
public String getResultat() {
return this.resultat;
}
public void setResultat(String resultat) {
this.resultat = resultat;
}
public boolean controlerAdmin(HttpServletRequest request) {
User admin = new User("Lovelace", "Ada");
User userSaisi = new User(request.getParameter("pseudo"), request.getParameter("mdp"));
boolean estAdmin = admin.equals(userSaisi);
if (estAdmin) {
this.setResultat("Vous êtes administrateur");
} else {
this.setResultat("Vous n'êtes pas administrateur");
}
return estAdmin;
}
}

View File

@@ -21,8 +21,6 @@ import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "AccueilServlet", urlPatterns = {"/Accueil"})
public class AccueilServlet extends HttpServlet {
String monPseudo = "steve.mngn";
String monMdp = "1234";
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
@@ -81,14 +79,6 @@ public class AccueilServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pseudo = (String) request.getParameter("pseudo");
String mdp = (String) request.getParameter("mdp");
if (!pseudo.isBlank() && !mdp.isBlank()) {
if (pseudo.equals(this.monPseudo) && mdp.equals(this.monMdp)) {
request.setAttribute("login", true);
}
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/accueilVue.jsp");
dispatcher.forward(request, response);
}

View File

@@ -0,0 +1,110 @@
/*
* 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 jakarta.servlet.RequestDispatcher;
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 steve.maingana
*/
@WebServlet(name = "AuthentifServlet", urlPatterns = {"/authentification"})
public class AuthentifServlet extends HttpServlet {
// String monPseudo = "Lovelace";
String monPseudo = "steve";
String monMdp = "Ada";
/**
* 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 AuthentifServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet AuthentifServlet 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 {
String pseudo = (String) request.getParameter("pseudo");
String mdp = (String) request.getParameter("mdp");
if (!pseudo.isBlank() && !mdp.isBlank()) {
if (pseudo.equals(this.monPseudo) && mdp.equals(this.monMdp)) {
request.setAttribute("login", true);
}
}
AuthentifForm authentification = new AuthentifForm();
boolean isAdmin = authentification.controlerAdmin(request);
request.setAttribute("authentification", authentification);
request.setAttribute("admin", isAdmin);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/accueilVue.jsp");
dispatcher.forward(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

@@ -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;
/**
*
@@ -59,7 +62,11 @@ public class ListeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
ClientMysql clientDB = new ClientMysql();
ArrayList<Client> clients = clientDB.readAll();
request.setAttribute("clients", clients);
getServletContext().getRequestDispatcher("/WEB-INF/listeClientsVue.jsp").forward(request, response);
}

View File

@@ -11,12 +11,23 @@
<%@include file="jspf/entete.jspf" %>
<body>
<%@include file="jspf/menu.jspf" %>
<h1>Bienvenue sur la nouvelle application de gestion des clients écrite avec JakartaEE</h1>
<h1>Gestion de clients</h1>
<c:choose>
<c:when test="${!login}"><%@ include file="jspf/login.jspf" %></c:when>
<c:when test="${login}">
<p>Bienvenue ${param.pseudo} !</p>
<c:choose>
<c:when test="${!login}">
<p>${authentification.getResultat()}</p>
</c:when>
<c:when test="${login}">
<p>${authentification.getResultat()}</p>
</c:when>
</c:choose>
</c:when>
</c:choose>
<p>
${login}
</p>
</body>
</html>

View File

@@ -4,4 +4,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Gestion des clients</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/style/style.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Comic+Relief:wght@400;700&display=swap');
</style>
</head>

View File

@@ -1,6 +1,6 @@
<%-- any content can be specified here e.g.: --%>
<%@ page pageEncoding="UTF-8" %>
<form method="POST" action="/Test2Jakarta/Accueil">
<form 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

@@ -4,6 +4,7 @@
Author : steve.maingana
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
@@ -11,6 +12,10 @@
<body>
<%@include file="jspf/menu.jspf" %>
<h1>Liste des clients</h1>
<p>Page en construction</p>
<ul>
<c:forEach var="client" items="${clients}">
<li>${client.getNom()}</li>
</c:forEach>
</ul>
</body>
</html>

View File

@@ -12,6 +12,10 @@
<servlet-name>ListeServlet</servlet-name>
<servlet-class>com.test.servlets.ListeServlet</servlet-class>
</servlet>
<servlet>
<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>
@@ -28,6 +32,10 @@
<servlet-name>ListeServlet</servlet-name>
<url-pattern>/ListeClients</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AuthentifServlet</servlet-name>
<url-pattern>/authentification</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30

View File

@@ -6,32 +6,53 @@ Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/CascadeStyleSheet.css
Created on : 15 sept. 2025, 14:59:25
Author : steve.maingana
*/
:root {
--primary-color: #cc0000;
--secondary-color: #990000;
--font-style: "Comic Relief", system-ui;
}
body {
background-color: whitesmoke;
margin: 0;
font-style: var(--font-style);
}
nav {
display: flex;
background-color: var(--primary-color);
justify-content: center;
align-items: center;
width: 100%;
padding: 1.2rem;
top: 0;
left: 0;
width: 100%;
}
nav ul {
color: white;
list-style: none;
display: flex;
gap: 1rem;
}
nav ul li {
nav a {
text-decoration: none;
color: white;
}
h1 {
text-align: center;
font-weight: bold;
}
form {
margin: 2rem;
background: #333333;
text-align: center;
margin: 8rem;
padding: 2rem;
background-color: var(--secondary-color);
border-radius: 8px;
border-style: none;
}
form label {
margin: auto;
padding-bottom: 1rem;
color: white;
font-weight: bold;
}