Advertisement
AnindyaBiswas

puspa

Jan 19th, 2023
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. package pkg;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9.  
  10. import javax.servlet.ServletContext;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. /**
  18.  * Servlet implementation class Login
  19.  */
  20. @WebServlet("/Login")
  21. public class Login extends HttpServlet {
  22.     private static final long serialVersionUID = 1L;
  23.  
  24.     /**
  25.      * Default constructor.
  26.      */
  27.     public Login() {
  28.         // TODO Auto-generated constructor stub
  29.     }
  30.  
  31.     /**
  32.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  33.      */
  34.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35.         // TODO Auto-generated method stub
  36.        
  37.         request.setAttribute("wrong_password", "hidden");
  38.         request.setAttribute("wrong_email", "hidden");
  39.        
  40.         request.getRequestDispatcher("login.jsp").include(request, response);
  41.        
  42.     }
  43.  
  44.     /**
  45.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  46.      */
  47.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  48.         // TODO Auto-generated method stub
  49.        
  50.         ServletContext context=getServletContext();
  51.        
  52.         response.setContentType("text/html");
  53.         PrintWriter pw = response.getWriter();
  54.        
  55.        
  56.         String email = request.getParameter("email");
  57.         String password= request.getParameter("password");
  58.        
  59.         System.out.println(email + " " + password);
  60.        
  61.         try {
  62.            
  63.             Connection con;
  64.            
  65.             Class.forName("com.mysql.cj.jdbc.Driver");
  66.             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/exam", "root", "abcd");
  67.            
  68.             PreparedStatement pstm;
  69.            
  70.             pstm = con.prepareStatement("select email, password, name from user_table where email = ?");
  71.             pstm.setString(1, email);
  72.            
  73.             ResultSet rs = pstm.executeQuery();
  74.            
  75.             if(rs.next())
  76.             {
  77.                 if(rs.getString("password").equals(password))
  78.                 {
  79.                     System.out.println("Pwd correct");
  80.                    
  81.                     pw.println("<h1>Welcome </h1>" + rs.getString("name"));
  82.                 }
  83.                 else
  84.                 {
  85.                     System.out.println("Pwd incorrect");
  86.                     request.setAttribute("wrong_email", "hidden");
  87.                     request.getRequestDispatcher("login.jsp").include(request, response);
  88.                    
  89.                 }
  90.                    
  91.             }
  92.            
  93.             else
  94.             {
  95.                 System.out.println("Email dosen't exist");
  96.                 request.setAttribute("wrong_password", "hidden");
  97.                 request.getRequestDispatcher("login.jsp").include(request, response);
  98.             }
  99.            
  100.            
  101.         }catch(Exception e) {System.out.println(e);}
  102.     }
  103.  
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement