Guest User

Untitled

a guest
Nov 18th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. $.ajax({
  2. url: "LoginServlet",
  3. type: "post",
  4. data: {
  5. "username": username,
  6. "password": password
  7. },
  8. cache: false,
  9. success: function(data) {
  10. var xhr = data.msg;
  11. if (xhr === "true") {
  12. alert(xhr);
  13. window.location.href = "success.jsp";
  14.  
  15. } else {
  16. alert(xhr);
  17. }
  18. }
  19.  
  20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
  21. LoginDAO dao = new LoginDAO();
  22. String username=request.getParameter("username");
  23. String password=request.getParameter("passsword");
  24. String msg = "false";
  25.  
  26.  
  27.  
  28.  
  29. Login login = dao.validateUser(username, password);
  30.  
  31. String username1 = login.getUsername();
  32. String password1 = login.getPassword();
  33. if(LoginDAO.isEqual(username, password1) && LoginDAO.isEqual(password, password1)){
  34.  
  35. msg = "true";
  36.  
  37. }
  38.  
  39.  
  40. response.setContentType("text/plain");
  41. response.getWriter().write(msg);
  42.  
  43.  
  44. }
  45.  
  46. <script>
  47. $(document).ready(function() {
  48. $("button").click(function() {
  49. var username = "admin";
  50. var password = "admin";
  51. $.ajax({
  52. url: "LoginServlet",
  53. type: "post",
  54. data: {
  55. "username": username,
  56. "password": password
  57. },
  58. cache: false,
  59. success: function(data) {
  60. console.log("data: " + data);
  61. var xhr = data;
  62. console.log("xhr: " + xhr);
  63. if (data == "true") {
  64. alert(xhr);
  65. window.location.href = "success.jsp";
  66.  
  67. } else {
  68. alert(xhr);
  69. }
  70. }
  71. });
  72. });
  73.  
  74. });
  75. </script>
  76.  
  77. String username = request.getParameter("username");
  78. String password = request.getParameter("password");
  79.  
  80. response.setContentType("text/plain");
  81. response.getWriter().write(msg);
  82.  
  83. $.ajax({
  84. url: "LoginServlet",
  85. type: "post",
  86. data: {
  87. "username": username,
  88. "password": password
  89. },
  90. dataType: "text", // To match with the actual response type: «text/plain».
  91. cache: false,
  92. success: function (data) {
  93. var xhr = data; // Because «data» contains true or false.
  94. if (xhr === "true") {
  95. alert(xhr);
  96. window.location.href = "success.jsp";
  97.  
  98. } else {
  99. alert(xhr);
  100. }
  101. }
  102. });
Add Comment
Please, Sign In to add comment