SHARE
TWEET

Untitled

a guest Jan 26th, 2019 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <a href="#"><button id="cus_login" class="info" type="button">Login</button></a>
  2.    
  3. $(document).ready(function() {
  4. $('#cus_login').click(function() {
  5.  
  6.      var username = $('#cus_name').val();
  7.      var pw = $('#cus_password').val();
  8.      var method = "one";
  9.  
  10.      $.ajax({
  11.          type: "POST",
  12.          data: {
  13.              method: method,
  14.              username: username,
  15.              pw: pw
  16.          },
  17.          url: 'LoginServlet',
  18.          success: function(result) {
  19.              if(result) {
  20.                  window.location = 'cust_profile.html'
  21.                  console.log(result);
  22.              }
  23.              else{
  24.                  alert("Login failure, please check and
  25. try again");
  26.              }
  27.          },
  28.          error, function(result) {
  29.              console.log(result);
  30.              alert("Connection could not be established.
  31. Please try again.");
  32.          }
  33.      });
  34.  });
  35. });
  36.    
  37. import util.DBUtil;
  38.  
  39. @WebServlet("/LoginServlet")
  40. public class LoginServlet extends HttpServlet {
  41. private static final long serialVersionUID = 1L;
  42.  
  43. DBUtil datasrc = new DBUtil();
  44. Connection conn = datasrc.getConn();
  45.  
  46. protected void doPost(HttpServletRequest request, HttpServletResponse
  47. response) throws ServletException, IOException {
  48.  
  49.     response.setContentType("text/plain");
  50.     System.out.println("--------------------------/nDSConn
  51. established, doGet method called");
  52.     String method = request.getParameter("method");
  53.     String username = request.getParameter("username");
  54.     String pw = request.getParameter("pw");
  55.     boolean finalstatus = false;
  56.     boolean finalstatus1 = false;
  57.  
  58.     if(usernameCheck(username) == true && passwordCheck(pw) ==
  59. true) {
  60.         switch(method) {
  61.         case "1":
  62.             finalstatus = custLogin(username, pw);
  63.         case "2":
  64.             finalstatus1 = busLogin(username, pw);
  65.         }
  66.     }
  67.  
  68.  
  69.     if(finalstatus) {
  70.         HttpSession session = request.getSession();
  71.         session.setAttribute("username", username);
  72.         response.sendRedirect("cust_profile.jsp");;
  73.     }
  74.     else if(finalstatus1) {
  75.         HttpSession session = request.getSession();
  76.         session.setAttribute("username", username);
  77.         response.sendRedirect("bus_profile.jsp");
  78.     }
  79.     else {
  80.         response.sendError(001, "Unsuccessful");
  81.     }
  82. }
  83.  
  84. private boolean usernameCheck(String username) {
  85.     if(username.matches("^[0-9]*$") && username.length() == 8) {
  86.         return true;
  87.     }
  88.     else return false;      
  89. }
  90.  
  91. private boolean passwordCheck(String pw) {
  92.     if(pw.length() <= 15) {
  93.         return true;
  94.     }
  95.     else return false;
  96. }
  97. private boolean custLogin(String un, String pw) {
  98.     PreparedStatement cps = null;
  99.     try {
  100.         String custsql = ("select * from fyp where cust_id = "
  101. + un + " password = " + pw);
  102.         cps = conn.prepareStatement(custsql);
  103.         ResultSet rs = cps.executeQuery();
  104.  
  105.         while(rs.next()) {
  106.             System.out.println("Login successful");
  107.             return true;
  108.         }
  109.     }
  110.     catch (Exception e) {
  111.         System.out.println("Login failure: ");
  112.     }
  113.     return false;
  114.  
  115. }
  116. }
  117.    
  118. public class DBUtil extends HttpServlet {
  119.  
  120. static DataSource ds=null;
  121.  
  122. static {
  123.     try {
  124.         System.out.println("----------------------------------------------"
  125.                 + "/n DSConn initialised - method called to estabish DS");
  126.         Context initContext = new InitialContext();
  127.         Context envContext = (Context) initContext.lookup("java:/comp/env");
  128.         ds = (DataSource) envContext.lookup("jdbc/fyp");
  129.  
  130.         System.out.println(ds + " DS has been established via JDNI lookup");
  131.        }
  132.  
  133.        catch(Exception e) {
  134.            System.out.println("Problem establishing DS");
  135.            e.printStackTrace();
  136.        }
  137.     }
  138.  
  139. public Connection getConn() {
  140.     try {
  141.         return ds.getConnection();
  142.     }
  143.     catch (SQLException e) {
  144.         System.out.println("error");
  145.     }
  146.     return null;
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top