Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pkg;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class Login
- */
- @WebServlet("/Login")
- public class Login extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * Default constructor.
- */
- public Login() {
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- request.setAttribute("wrong_password", "hidden");
- request.setAttribute("wrong_email", "hidden");
- request.getRequestDispatcher("login.jsp").include(request, response);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- ServletContext context=getServletContext();
- response.setContentType("text/html");
- PrintWriter pw = response.getWriter();
- String email = request.getParameter("email");
- String password= request.getParameter("password");
- System.out.println(email + " " + password);
- try {
- Connection con;
- Class.forName("com.mysql.cj.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/exam", "root", "abcd");
- PreparedStatement pstm;
- pstm = con.prepareStatement("select email, password, name from user_table where email = ?");
- pstm.setString(1, email);
- ResultSet rs = pstm.executeQuery();
- if(rs.next())
- {
- if(rs.getString("password").equals(password))
- {
- System.out.println("Pwd correct");
- pw.println("<h1>Welcome </h1>" + rs.getString("name"));
- }
- else
- {
- System.out.println("Pwd incorrect");
- request.setAttribute("wrong_email", "hidden");
- request.getRequestDispatcher("login.jsp").include(request, response);
- }
- }
- else
- {
- System.out.println("Email dosen't exist");
- request.setAttribute("wrong_password", "hidden");
- request.getRequestDispatcher("login.jsp").include(request, response);
- }
- }catch(Exception e) {System.out.println(e);}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement