Guest User

Untitled

a guest
Sep 1st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. Manipulate Login progress from Servlet -> JavaScript
  2. * the Apache server
  3. * DB on Eclipse
  4. * one index.html file with Javascript
  5. * one servlet
  6.  
  7. * get a credential information
  8. * send via method of 'Javascript -> servlet' to find out user exist.
  9. * from the Javascript method need to receive DB existence.
  10.  
  11. * I used 'AJAX', looks to me all same as one of Javascript method that does do for asynchronous with HTML and server.
  12. * from the index.html page, if I press wrong information (id, password) then yes it prompts the message.
  13.  
  14. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  15. {
  16. String name = request.getParameter("user_id");
  17. String password = request.getParameter("passwd");
  18. // search for query user existence
  19. try
  20. {
  21. // TODO session, cookies
  22. sharedConnection = dataSource.getConnection();
  23. Statement stmt = sharedConnection.createStatement();
  24. String str = "select * from customers where email ='" + name + "' and password='" + password + "' ";
  25. ResultSet rs = stmt.executeQuery(str);
  26. HttpSession session = request.getSession();
  27.  
  28. // not found
  29. if (!rs.next())
  30. {
  31. PrintWriter out = response.getWriter();
  32. out.println("<h3>credentials invalid</h3>");
  33. }
  34. // exist
  35. else
  36. {
  37. String first_name = rs.getString("first_name");
  38. String last_name = rs.getString("last_name");
  39. session.setMaxInactiveInterval(1800); //set timeout to 30min
  40. session.setAttribute("loggedin", true);
  41. session.setAttribute("name", (first_name+ ", " + last_name));
  42. response.sendRedirect("/MovieDB/main.html");
  43. }
  44. }
  45. catch(SQLException e)
  46. {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50. }
  51.  
  52. <script type="text/javascript">
  53. function loadXMLDoc()
  54. {
  55. var xmlhttp;
  56. var user_id = document.getElementById('user_id').value;
  57. var passwd = document.getElementById('passwd').value;
  58.  
  59. <!-- newer browser -->
  60. if (window.XMLHttpRequest)
  61. {
  62. xmlhttp=new XMLHttpRequest();
  63. }
  64. <!-- IE 5, 6 -->
  65. else
  66. {
  67. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  68. }
  69.  
  70. xmlhttp.onreadystatechange=function()
  71. {
  72. if (xmlhttp.readyState==4)
  73. {
  74. if (xmlhttp.status==200)
  75. document.getElementById("state_msg").innerHTML=xmlhttp.responseText;
  76.  
  77. }
  78. }
  79. <!-- access servlet with parameters -->
  80. xmlhttp.open("POST", ("LoginServlet?user_id=" + user_id +"&passwd=" + passwd) , true);
  81. xmlhttp.send();
  82.  
  83. <!-- If Servlet don't detour main.html then we must assume error -->
  84. var err = document.getElementById("state_msg");
  85. err.style.display = "block";
  86. }
  87.  
  88. </script>
  89. <title>MovieDB : WebAuth</title>
  90. </head>
  91. <body>
  92. <h1>FabFlix WebAuth</h1>
  93. <!-- label user id -->
  94. <div id=input_info>
  95. <label for="user_id">User :</label>
  96. </div>
  97. <!-- user id -->
  98. <input type="text" id="user_id" name="user_id" />
  99. <p></p>
  100. <!-- password label -->
  101. <div id=input_info>
  102. <label for="passwd">Password :</label>
  103. </div>
  104. <!-- password -->
  105. <input type="password" id="passwd" name="passwd" />
  106. <!-- submit button -->
  107. <p></p>
  108. <div id="button_position" align="center">
  109. <button type="button" onclick="loadXMLDoc()">Login</button>
  110. </div>
  111. <div id="state_msg" style="display:none"></div>
  112. </body>
  113.  
  114. response.sendRedirect("/MovieDB/main.html");
  115.  
  116. out.println("/MovieDB/main.html");
Add Comment
Please, Sign In to add comment