Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. <html>
  2. <head>
  3. <!-- more scripts / google api js are here -->
  4. <script type="text/javascript">
  5. function get_action(form) {
  6. var v = grecaptcha.getResponse();
  7. if(v.length === 0) {
  8. document.getElementById('captcha').innerHTML="Login failed: Empty captcha";
  9. return false;
  10. } else {
  11. return true;
  12. }
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. <form action="login" method="post" onsubmit="return get_action(this);">
  18. <input type="text" id="email" value="${fn:escapeXml(param.email)}" required>
  19. <input type="text" id="password" value="${fn:escapeXml(param.password)}" required>
  20. <div class="g-recaptcha" data-sitekey="xxx"></div>
  21. <input class="submit_button" type="submit" name="submit" value="Submit" />
  22. <span class="error"${error.invalid}</span>
  23. <div id="captcha" class="captchaError"></div>
  24. </form>
  25. </body>
  26. </html>
  27.  
  28. @WebServlet("/login")
  29. public class LoginServlet extends HttpServlet {
  30.  
  31. private LoginDAO loginDAO;
  32.  
  33. @Override
  34. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. request.getRequestDispatcher("login.jsp").forward(request,response);
  37. }
  38.  
  39. @Override
  40. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  41. throws ServletException, IOException {
  42. Map<String, String> error = new HashMap<String,String>();
  43. request.setAttribute("error",error);
  44.  
  45. String email = request.getParameter("email");
  46. String password = request.getParameter("password");
  47.  
  48. // Verify re-captcha
  49. String gRecaptchaResponse = request.getParameter("captcha");
  50. boolean verify = VerifyRecaptcha.verify(gRecaptchaResponse);
  51. if(!verify) {
  52. error.put("captcha","You seem to be a robot. Try to use the captcha again");
  53. }
  54. if(error.isEmpty()) {
  55. loginDAO = new LoginDAO();
  56. try {
  57. List<Customer> customer = new ArrayList<Customer>();
  58. customer = loginDAO.validate(email,password);
  59. if(customer.isEmpty()) {
  60. error.put("invalid","Invalid email or password");
  61. }
  62. if(error.isEmpty()) { // no errors, proceed
  63. HttpSession session = request.getSession(true);
  64. Customer user = customer.get(0);
  65. session.setAttribute("user",user);
  66. response.sendRedirect("main");
  67. return;
  68. }
  69. request.getRequestDispatcher("login").forward(request,response);
  70. } catch(SQLException e) {
  71. throw new ServletException("Could not check login",e);
  72. }
  73. loginDAO.closeLoginDAO();
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement