Merge origin/master

Conflicts:
	web/WEB-INF/AuthentificationJSP.jsp
	web/WEB-INF/ProfilJSP.jsp
This commit is contained in:
elliass.chelloug 2021-10-20 11:46:39 +02:00
parent 1e8257677c
commit c7df279eab
3 changed files with 123 additions and 133 deletions

View File

@ -7,12 +7,17 @@ package bdd;
import com.mysql.cj.xdevapi.Client;
import com.test.beans.Pompier;
import com.test.forms.MD5;
import jakarta.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
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;
/**
*
@ -27,133 +32,25 @@ public class PompierMysql {
theConnection = Connexion.getConnect("localhost", "sdis29", "admin", "minda");
}
public ArrayList<Pompier> readAll() {
ArrayList<Pompier> lesPompiers = new ArrayList<>();
public boolean readPompier(HttpServletRequest request) {
boolean reponse = false;
Statement stmt;
String userSaisi = request.getParameter("ztPseudo");
String mdpSaisi = request.getParameter("ztMDP");
String mdpChiffre = MD5.encode(mdpSaisi);
System.out.println("userSaisi : "+ userSaisi);
System.out.println("mdpSaisi : "+ mdpChiffre);
try {
Statement stmt = theConnection.createStatement();
System.out.println("Select * FROM pompier WHERE login = '"+userSaisi+"' AND mdp = '"+mdpChiffre+"';");
stmt = theConnection.createStatement();
ResultSet resultQ = null;
resultQ = stmt.executeQuery("SELECT * FROM client");
while (resultQ.next()) {
unPompier = new Pompier(resultQ.getInt("id"),
resultQ.getString("nom"),
resultQ.getString("prenom"),
resultQ.getString("statut"),
resultQ.getString("typePers"),
resultQ.getString("mail"),
resultQ.getString("login"),
resultQ.getString("mdp"),
resultQ.getString("adresse"),
resultQ.getInt("cp"),
resultQ.getString("ville"),
resultQ.getInt("bip"),
resultQ.getInt("nbGardes"),
resultQ.getInt("grade"),
resultQ.getString("commentaire"),
resultQ.getString("dateEnreg"),
resultQ.getString("dateModif"));
lesPompiers.add(unPompier);
}
resultQ.close();
stmt.close();
//theConnection.close();
resultQ = stmt.executeQuery("Select * FROM pompier WHERE login = '"+userSaisi+"' AND mdp = '"+mdpChiffre+"';");
reponse = resultQ.next();
} catch (SQLException ex) {
System.out.println("SQLException : " + ex.getMessage());
System.out.println("SQLState : " + ex.getSQLState());
System.out.println("Code erreur : " + ex.getErrorCode());
Logger.getLogger(PompierMysql.class.getName()).log(Level.SEVERE, null, ex);
}
return lesPompiers;
}
/**
* Creation du client passé en paramètre dans la table client Requête non
* préparée
*
* @param c objet de type Client (sans identifiant)
* @return int : id du client créé
*/
public int create(Pompier p) {
int id = -1;
try {
Statement stmt = theConnection.createStatement();
int status = stmt.executeUpdate(
"INSERT INTO pompier (nom, prenom, statut, mail, login, mdp, adresse, cp, ville, bip, nbGardes, grade, commentaire, dateEnreg, dateModif) "
+ "VALUES ('" + p.getNom() + "', '"
+ p.getPrenom() + "', '"
+ p.getStatut() + "', "
+ p.getMail() + ", '"
+ p.getLogin() + "', '"
+ p.getMdp() + "', '"
+ p.getAdresse() + "', '"
+ p.getVille() + "', '"
+ p.getBip() + "', '"
+ p.getNbGardes() + "', '"
+ p.getGrade() + "', '"
+ p.getCommentaire() + "', '"
+ p.getDateEnreg() + "', '"
+ p.getDateModif() + "');",
Statement.RETURN_GENERATED_KEYS);
// Recherche de l'identifiant du client créé
if (status > 0) {
ResultSet result = stmt.getGeneratedKeys();
if (result.first()) {
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 client passé en paramètre dans la table client Requête
* préparée
*
* @param c objet de type Client (sans identifiant)
* @return int : id du client créé
*/
public int createRP(Pompier p) {
int id = -1;
try {
PreparedStatement stmt = null;
String sql = "INSERT INTO pompier (nom, prenom, statut, mail, login, mdp, adresse, cp, ville, bip, nbGardes, grade, commentaire, dateEnreg, dateModif) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
stmt = theConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, p.getNom());
stmt.setString(2, p.getPrenom());
stmt.setString(3, p.getStatut());
stmt.setString(4, p.getMail());
stmt.setString(5, p.getLogin());
stmt.setString(6, p.getMdp());
stmt.setString(7, p.getAdresse());
stmt.setString(8, p.getVille());
stmt.setInt(9, p.getBip());
stmt.setInt(10, p.getNbGardes());
stmt.setInt(11, p.getGrade());
stmt.setString(12, p.getCommentaire());
stmt.setString(13, p.getDateEnreg());
stmt.setString(14, p.getDateModif());
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);
}
}
} catch (SQLException ex) {
System.out.println("SQLException : " + ex.getMessage());
System.out.println("SQLState : " + ex.getSQLState());
System.out.println("Code erreur : " + ex.getErrorCode());
}
return id;
return reponse;
}
}

View File

@ -5,6 +5,7 @@
*/
package com.test.forms;
import bdd.PompierMysql;
import com.test.beans.User;
import jakarta.servlet.http.HttpServletRequest;
@ -24,16 +25,10 @@ public class AuthentifForm {
this.resultat = resultat;
}
public boolean controlerAdmin(HttpServletRequest request) {
String userSaisi = request.getParameter("ztPseudo");
String mdpSaisi = request.getParameter("ztMDP");
String mdpChiffre = MD5.encode(mdpSaisi);
String sql=
return ;
public boolean authentifPompier(HttpServletRequest request)
{
PompierMysql pms = new PompierMysql();
return pms.readPompier(request);
}
}

View File

@ -0,0 +1,98 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.test.servlets;
import com.test.forms.AuthentifForm;
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;
/**
*
* @author Dominique_2
*/
public class AuthentifServlet 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 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);
getServletContext().getRequestDispatcher("/WEB-INF/AuthentificationJSP.jsp").forward(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 {
// Création de l'objet leControle de type AuthentifForm
AuthentifForm leControle = new AuthentifForm();
// Appel de la méthode controlerAdmin
String isAdmin = leControle.AuthentifPompier(request);
// Création de 2 attributs de requête (isAdmin et leControle)
request.setAttribute("isAdmin", isAdmin);
request.setAttribute("controlForm", leControle);
// Affichage de la JSP
getServletContext().getRequestDispatcher("/WEB-INF/ProfilJSP.jsp")
.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>
}