Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package controller;
  7.  
  8. import business.User;
  9. import dataaccess.UserDB;
  10. import java.io.IOException;
  11. import java.sql.SQLException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.servlet.http.HttpSession;
  20.  
  21. @WebServlet(name = "membershipServlet", urlPatterns = {"/membership"})
  22. public class membershipServlet extends HttpServlet {
  23.  
  24. /**
  25. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  26. * methods.
  27. *
  28. * @param request servlet request
  29. * @param response servlet response
  30. * @throws ServletException if a servlet-specific error occurs
  31. * @throws IOException if an I/O error occurs
  32. */
  33. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  34. /**
  35. * Handles the HTTP <code>GET</code> method.
  36. *
  37. * @param request servlet request
  38. * @param response servlet response
  39. * @throws ServletException if a servlet-specific error occurs
  40. * @throws IOException if an I/O error occurs
  41. */
  42. @Override
  43. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  44. throws ServletException, IOException {
  45.  
  46. }
  47.  
  48. /**
  49. * Handles the HTTP <code>POST</code> method.
  50. *
  51. * @param request servlet request
  52. * @param response servlet response
  53. * @throws ServletException if a servlet-specific error occurs
  54. * @throws IOException if an I/O error occurs
  55. */
  56. @Override
  57. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  58. throws ServletException, IOException {
  59.  
  60. HttpSession session = request.getSession();
  61.  
  62. String message;
  63. String url = "/login.jsp";
  64.  
  65. String action = request.getParameter("action");
  66. if (action == null) {
  67. action = "start";
  68. }
  69. if (action.equals("start"))
  70. {
  71. url = "/login.jsp";
  72. } else if (action.equals("signup"))
  73. {
  74. String fullName = request.getParameter("fullname");
  75. String userName = request.getParameter("username");
  76. String email = request.getParameter("emailAddress");
  77. String password = request.getParameter("password");
  78. String Cpassword = request.getParameter("Cpassword");
  79. String birthdate = request.getParameter("birthdate");
  80. String questionNo = request.getParameter("questionNo");
  81. String answer = request.getParameter("answer");
  82.  
  83. User user;
  84. user = new User(fullName, userName, email, password, birthdate, questionNo, answer);
  85.  
  86. if(fullName == null || email == null || userName == null || Cpassword == null || password == null ||
  87. birthdate == null || answer == null || fullName.isEmpty() || userName.isEmpty() ||
  88. email.isEmpty() || password.isEmpty() || birthdate.isEmpty() || answer.isEmpty())
  89. {
  90. message = "please fill out all the fields";
  91. url = "/signup.jsp";
  92. }
  93.  
  94. if (password != Cpassword)
  95. {
  96. message = "please make sure passwords are the same";
  97. url = "/signup.jsp";
  98. }
  99.  
  100. if (UserDB.search(user.getEmail()) != null) {
  101. message = "this email address already exists. <br>"
  102. + "Please enter another email address";
  103. url = "/login.jsp";
  104. }
  105. else
  106. {
  107. try {
  108. UserDB.insert(user);
  109. url = "/home.jsp";
  110. } catch (SQLException ex) {
  111. Logger.getLogger(membershipServlet.class.getName()).log(Level.SEVERE, null, ex);
  112. } catch (ClassNotFoundException ex) {
  113. Logger.getLogger(membershipServlet.class.getName()).log(Level.SEVERE, null, ex);
  114. }
  115.  
  116. }
  117.  
  118. }
  119.  
  120. getServletContext()
  121. .getRequestDispatcher(url)
  122. .forward(request, response);
  123.  
  124. }
  125.  
  126. /**
  127. * Returns a short description of the servlet.
  128. *
  129. * @return a String containing servlet description
  130. */
  131. @Override
  132. public String getServletInfo() {
  133. return "Short description";
  134. }// </editor-fold>
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement