|
| 1 | +package com.daytwo; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.PrintWriter; |
| 5 | +import java.sql.Connection; |
| 6 | +import java.sql.PreparedStatement; |
| 7 | +import java.sql.ResultSet; |
| 8 | +import java.sql.SQLException; |
| 9 | + |
| 10 | +import javax.servlet.GenericServlet; |
| 11 | +import javax.servlet.ServletException; |
| 12 | +import javax.servlet.ServletRequest; |
| 13 | +import javax.servlet.ServletResponse; |
| 14 | + |
| 15 | +import com.daytwo.bean.Customer; |
| 16 | +import com.util.DBUtil; |
| 17 | + |
| 18 | +public class LoginServletGetParamsUsingBeans extends GenericServlet { |
| 19 | + |
| 20 | + /** |
| 21 | + * |
| 22 | + */ |
| 23 | + private static final long serialVersionUID = 1L; |
| 24 | + |
| 25 | + @Override |
| 26 | + public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { |
| 27 | + |
| 28 | + res.setContentType("text/html"); |
| 29 | + PrintWriter pw = res.getWriter(); |
| 30 | + Customer customer = new Customer(); |
| 31 | + // req.getParameter |
| 32 | + String email = req.getParameter("email"); |
| 33 | + String password = req.getParameter("pwd"); |
| 34 | + customer.setEmail(email); |
| 35 | + customer.setPwd(password); |
| 36 | + |
| 37 | + pw.println("Hello " + email + ", wait while we are checking your credentials..<br>"); |
| 38 | + |
| 39 | + boolean isLoginSuccess = false; |
| 40 | + // isLoginSuccess = email.equals("shashi@gmail.com") && |
| 41 | + // password.equals("shashi"); |
| 42 | + |
| 43 | + Connection connection = DBUtil.getConnection(); |
| 44 | + if (connection == null) { |
| 45 | + pw.println("<br>Server is down, try after some time!!"); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + String sqlQueryToCheckIfCustomerExistsWithReceivedEmailAndPassword = "select * from customer where email = ? and password = ?"; |
| 50 | + try { |
| 51 | + PreparedStatement ps = connection |
| 52 | + .prepareStatement(sqlQueryToCheckIfCustomerExistsWithReceivedEmailAndPassword); |
| 53 | + |
| 54 | + ps.setString(1, customer.getEmail()); |
| 55 | + ps.setString(2, customer.getPwd()); |
| 56 | + |
| 57 | + ResultSet rs = ps.executeQuery(); |
| 58 | + |
| 59 | + if (rs.next()) { |
| 60 | + isLoginSuccess = true; |
| 61 | + |
| 62 | + String id = rs.getString("id"); |
| 63 | + String name = rs.getString("name"); |
| 64 | + String dob = rs.getString("dob"); |
| 65 | + email = rs.getString("email"); |
| 66 | + String mob = rs.getString("mob"); |
| 67 | + String gender = rs.getString("gender"); |
| 68 | + customer.setId(id); |
| 69 | + customer.setEmail(email); |
| 70 | + pw.println("<ul>" + "<li>Name: " + name + "</li>" + "<li>Id: " + id + "</li>" + "<li>Email: " + email |
| 71 | + + "</li>" + "<li>DOB: " + dob + "</li>" + "<li>Mob: " + mob + "</li>" + "<li>Gender: " + gender |
| 72 | + + "</li>" + "</ul>"); |
| 73 | + |
| 74 | + } else { |
| 75 | + isLoginSuccess = false; |
| 76 | + } |
| 77 | + |
| 78 | + } catch (SQLException e) { |
| 79 | + isLoginSuccess = false; |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + |
| 83 | + if (isLoginSuccess) { |
| 84 | + // login success |
| 85 | + pw.println("<h1>User Login Success</h1>"); |
| 86 | + |
| 87 | + // |
| 88 | + } else { |
| 89 | + // login failure |
| 90 | + pw.println("<h2>Invalid Credentials, Try Again!!"); |
| 91 | + // include the error in same page and show the login form again |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments