1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
@WebServlet(name = "Login", urlPatterns = {"/enregistrement"})
public class Login extends HttpServlet {
private static final String CONTENT_TYPE ="text/html; charset=UTF-8";
private String name;
private String password;
public static Connection conn;
public static String UR;
public static String IPHOST = "127.0.0.1";//adresse de bouclage
public static String URL_DB = "jdbc:mysql://localhost:3306/dossier_Camer-Co";
public static String USERNAME_DB = "root";
public static String PASSWORD_DB = "";
public static int PORT = 11111;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType(CONTENT_TYPE);
name = request.getParameter("user");
password = request.getParameter("pass");
boolean result = checkPassword(name,password);
Statement stmt = null;
ResultSet res = null;
if (result == true){
loadDatabase();
try {
res = stmt.executeQuery("INSERT INTO utilisateurs(nom, password) ('paul', 'paul');");
} catch (SQLException e) {
out.println("erreur d acces a la bd");
}
//this.getServletContext().getRequestDispatcher("/WEB-INF/Accueil.jsp").forward(request, response);
}
else{
if(name != null && password != null){
out.println("mot de passe ou identifiant invalide");
}
else{
}
}
}
protected boolean checkPassword (String user, String password){
String correctPassword = null;
Statement statement = null;
ResultSet rs = null;
ResultSet res = null;
loadDatabase();
try{
statement = conn.createStatement();
//Execution de la requete
rs = statement.executeQuery("select password from utilisateurs where nom ='"+user+"'");
if (statement == null){
System.out.println("connexion unsuccesful");
}
if(rs.next()){
correctPassword = rs.getString(1);
}
if(correctPassword.equals(password)){
return true;
}
else{
return false;
}
}
catch(Exception e){
System.out.println("une exeption est survenue lors du mote de passe" +e.toString());
return false;
}
finally{
//fermature de la connexion
try {
if (rs != null)
rs.close();
if (statement != null)
statement.close();
if (conn != null)
conn.close();
} catch (SQLException ignore){
}
}
}
private void loadDatabase(){
// Chargement du driver
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("erreur lor du chargement du driver");
}
try {
conn = DriverManager.getConnection(URL_DB, USERNAME_DB, PASSWORD_DB);
}
catch (SQLException e){
e.printStackTrace();
}
} |
Partager