public class login_servlet extends HttpServlet { private static final long serialVersionUID = 1L; public login_servlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //this.getServletContext().getRequestDispatcher( "/login.jsp" ).forward( request, response ); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { java.sql.Connection connexion = null; java.sql.Statement statement = null; ResultSet resultat = null; String userQuery=""; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { } try { connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Dlala", "IPLS", "password"); } catch (SQLException e) { e.printStackTrace(); } try { statement = connexion.createStatement(); // Exécution de la requête resultat = statement.executeQuery("SELECT user FROM Client;"); // Récupération des données while (resultat.next()) { userQuery = resultat.getString("user"); } } catch (SQLException e) { } finally { // Fermeture de la connexion try { if (resultat != null) resultat.close(); if (statement != null) statement.close(); if (connexion != null) connexion.close(); } catch (SQLException ignore) { } } //************************************************// response.setContentType("text/html"); PrintWriter out = response.getWriter(); String n=request.getParameter("user"); String p=request.getParameter("pass"); HttpSession session = request.getSession(false); if(session!=null) session.setAttribute("name", n); if( n.equals(userQuery)){ RequestDispatcher rd=request.getRequestDispatcher("home.jsp"); rd.forward(request,response); } else{ out.print("<p style=\"color:red\">Sorry username or password error</p>"); RequestDispatcher rd=request.getRequestDispatcher("login.jsp"); rd.include(request,response); } out.close(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)