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
| import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Serv extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
/**Process the HTTP Get request*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String connectionURL = "jdbc:mysql://localhost/biblio";
Connection connection=null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String nom = req.getParameter("nom");
String passe = req.getParameter("passe");
String profession = req.getParameter("profession");
try {
// Load the database driver
Class.forName("org.gjt.mm.mysql.Driver");
// Pour se connecter à la database
connection = DriverManager.getConnection(connectionURL, "root", "rajae");
Statement commande = connection.createStatement();
ResultSet RS = commande.executeQuery("SELECT nom,passe,profession FROM client");
while(RS.next())
{
if (nom.equals(RS.getString("nom")) && passe.equals(RS.getString("passe")) && "client".equals(profession))
{
RequestDispatcher dispat = req.getRequestDispatcher("/client.jsp");
dispat.forward(req,res);
}
if (nom.equals(RS.getString("nom")) && passe.equals(RS.getString("passe")) && "responsable".equals(profession))
{
RequestDispatcher dispat = req.getRequestDispatcher("/responsable.jsp");
dispat.forward(req,res);
}
else
{
RequestDispatcher dispat = req.getRequestDispatcher("/erreur_login.jsp");
dispat.forward(req,res);
}
}
RS.close();
commande.close();
}
catch(ClassNotFoundException e){
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
out.println(e);
}
finally {
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
out.println(ignored);
}
}
}
} |
Partager