Authentification servlet jsp /mysql

Signaler
-
 scorpiontlili -
Bonjour,
quelqu'un peut m'aider a trouver un code source pour faire l'authentification ?
je veux la classe java du servlet et la page jsp
Merci infiniment.

1 réponse


Bonjour ,Tu peut suivre l' exemple suivant il contient la servlet , le DAO , La page JSP
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
    
public class DAO {
    public static Connection Con ;
static Statement St ;



public DAO(){
           

//String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/db";
String login="root";
String pswd="";
        
try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }
        try{
            Con = DriverManager.getConnection(url, login, pswd);
            System.out.println("OK connexion réussie...");
            
            St = Con.createStatement();

        }

        catch(SQLException ex){
            System.out.println(ex.getMessage());
        }
    }

    public Statement getStmt() {
        return St;
    }

    public Connection getCon() {
        return Con;
    }

}
        




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Authen;

import java.sql.*;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.*;
import javax.servlet.http.*;



@WebServlet(name "Action", urlPatterns {"/Action"})
public class Action extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>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");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Action</title>");
out.println("</head>");
out.println("");
out.println("Servlet Action at " + request.getContextPath () + "
");
out.println("");
out.println("</html>");
*/
} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* 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);
String UserName = request.getParameter("txtUserName");
String Password = request.getParameter("txtPassword");
DAO c = new DAO() ;
try {
ResultSet result = c.getStmt().executeQuery("select * from db.personne where email='"+UserName+"' and motpass='"+Password+"'");
result.first();
if(result.getRow()>0)
{
response.setContentType("text/html");
response.setCharacterEncoding( "UTF-8" );
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset="utf-8" />");
out.println("<title>Test</title>");
out.println("</head>");
out.println("");
out.println("ok

");
out.println("");
out.println("</html>");
}else
{
response.setContentType("text/html");
response.setCharacterEncoding( "UTF-8" );
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset="utf-8" />");
out.println("<title>Test</title>");
out.println("</head>");
out.println("");
out.println("non

");
out.println("");
out.println("</html>");

}
//this.getServletContext().getRequestDispatcher( "/WEB-INF/test.jsp" ).forward( request, response );
} catch (SQLException ex) {
response.setContentType("text/html");
response.setCharacterEncoding( "UTF-8" );
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset="utf-8" />");
out.println("<title>Test</title>");
out.println("</head>");
out.println("");
out.println(""+ex.getMessage()+"

");
out.println("");
out.println("</html>");

}

}

}

/**
* 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
*/





</code>
     
         
 
 
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "Authen.*"%> 

<!DOCTYPE html>
<html>
    <head>
        <SCRIPT language="Javascript"> 
       
    </SCRIPT>
            
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="shortcut icon" href="images/favicon.ico">
<link rel="stylesheet" type="text/css" href="css/style.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="css/popup.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="css/discussion.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="css/UI.css" media="screen"/>
<script src="js/jquery.js"></script>
<script src="js/script.js"></script>
<script src="js/easing.js"></script>
    </head>
   
    
        


[index.php ]




 


 









Gestion Offre de Formation








       
   
     
        <form METHOD="GET" ENCTYPE="text/plain" ACTION="Myservlet">
          
           
<label for="txtUserName">Email : </label>


<label for="txtPassword">Mot de passe: </label>


 
</form>
   

    
</html>


Bon Chance,