Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. CREATE TABLE IF NOT EXISTS user(
  2. user_id VARCHAR(255),
  3. user_password VARCHAR(255) NOT NULL,
  4. user_last_name VARCHAR(255),
  5. user_first_name VARCHAR(255),
  6. user_email VARCHAR(255) UNIQUE NOT NULL,
  7. user_type TINYINT UNSIGNED NOT NULL, /* VALUES: 0 - Guest, 1 - Admin, 2 - User */
  8. PRIMARY KEY(user_id)
  9. );
  10.  
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. String userId = request.getParameter("username");
  13. String userFirstName = request.getParameter("firstname");
  14. String userLastName = request.getParameter("lastname");
  15. String userEmail1 = request.getParameter("email1");
  16. String userEmail2 = request.getParameter("email2");
  17. String userPassword1 = request.getParameter("pass1");
  18. String userPassword2 = request.getParameter("pass2");
  19. String captchaAnswer = request.getParameter("answer");
  20.  
  21. try {
  22. // simple captcha
  23. HttpSession session = request.getSession(true);
  24. Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
  25. request.setCharacterEncoding("UTF-8");
  26.  
  27. boolean isCaptchaCorrect = captcha.isCorrect(captchaAnswer);
  28. session.setAttribute("isCaptchaCorrect", isCaptchaCorrect);
  29.  
  30. session.setAttribute("userId", userId);
  31. session.setAttribute("userFirstName", userFirstName);
  32. session.setAttribute("userLastName", userLastName);
  33. session.setAttribute("userEmail1", userEmail1);
  34. session.setAttribute("userEmail2", userEmail2);
  35.  
  36. if(isCaptchaCorrect) {
  37. // put database entries into a String[]
  38. DatabaseManipulator dm = new DatabaseManipulator();
  39. String[] usernameArray = dm.dbEntriesToArray("user_id");
  40. String[] emailArray = dm.dbEntriesToArray("user_email");
  41.  
  42. // validate inputs
  43. RegistrationModule rm = new RegistrationModule();
  44. boolean hasDuplicateUsername = rm.hasDuplicate(usernameArray, userId);
  45. boolean hasDuplicateEmail = rm.hasDuplicate(emailArray, userEmail1);
  46. boolean isEmailMatch = rm.isMatch(userEmail1, userEmail2);
  47. boolean isPasswordMatch = rm.isMatch(userPassword1, userPassword2);
  48.  
  49. // bind objects to session
  50. session.setAttribute("hasDuplicateUsername", hasDuplicateUsername);
  51. session.setAttribute("hasDuplicateEmail", hasDuplicateEmail);
  52. session.setAttribute("isEmailMatch", isEmailMatch);
  53. session.setAttribute("isPasswordMatch", isPasswordMatch);
  54.  
  55. // throw user-defined exceptions
  56. if(hasDuplicateUsername) {
  57. try {
  58. throw new UsernameAlreadyExistsException();
  59.  
  60. } catch(UsernameAlreadyExistsException uaee) {
  61. // redirect to result page
  62. response.sendRedirect("register-result.jsp");
  63. }
  64.  
  65. } else if(hasDuplicateEmail) {
  66. try {
  67. throw new EmailAlreadyExistsException();
  68.  
  69. } catch(EmailAlreadyExistsException eaee) {
  70. response.sendRedirect("register-result.jsp");
  71. }
  72.  
  73. } else if(!isEmailMatch) {
  74. try {
  75. throw new MismatchedEmailsException();
  76.  
  77. } catch(MismatchedEmailsException mee) {
  78. response.sendRedirect("register-result.jsp");
  79. }
  80.  
  81. } else if(!isPasswordMatch) {
  82. try {
  83. throw new MismatchedPasswordsException();
  84.  
  85. } catch(MismatchedPasswordsException mpe) {
  86. response.sendRedirect("register-result.jsp");
  87. }
  88.  
  89. // register success
  90. } else {
  91. // assign if match
  92. String userPassword = userPassword1;
  93. String userEmail = userEmail1;
  94.  
  95. // assemble user bean object
  96. User user = UserAssembler.getInstance(
  97. userId,
  98. userPassword,
  99. userLastName,
  100. userFirstName,
  101. userEmail,
  102. 2 // 2 = User
  103. );
  104.  
  105. // insert user into database
  106. dm.registerUser(user);
  107.  
  108. response.sendRedirect("register-result.jsp");
  109. }
  110.  
  111. // wrong captcha answer
  112. } else {
  113. response.sendRedirect("register-result.jsp");
  114. }
  115.  
  116. } catch(NullPointerException npe) {
  117. // redirect when servlet is illegally accessed
  118. response.sendRedirect("index.jsp");
  119. }
  120. }
  121.  
  122. try {
  123. throw new SomeException();
  124. } catch (SomeException uaee) {
  125. response.sendRedirect("some-result.jsp");
  126. }
  127.  
  128. response.sendRedirect("some-result.jsp");
  129.  
  130. if(hasDuplicateUsername) {
  131. response.sendRedirect("register-result.jsp");
  132. } else if(hasDuplicateEmail) {
  133. response.sendRedirect("register-result.jsp");
  134. } else if(!isEmailMatch) {
  135. response.sendRedirect("register-result.jsp");
  136. } else if(!isPasswordMatch) {
  137. response.sendRedirect("register-result.jsp");
  138. }
  139.  
  140. String userId = request.getParameter("username");
  141. String userFirstName = request.getParameter("firstname");
  142. String userLastName = request.getParameter("lastname");
  143. String userEmail1 = request.getParameter("email1");
  144. String userEmail2 = request.getParameter("email2");
  145. String userPassword1 = request.getParameter("pass1");
  146. String userPassword2 = request.getParameter("pass2");
  147. String captchaAnswer = request.getParameter("answer");
  148.  
  149. if (userId == null || userFirstName == null || userLastName == null ||
  150. userEmail1 == null || ...) {
  151. response.sendRedirect("index.jsp");
  152. return;
  153. }
  154.  
  155. try {
  156. throw new UsernameAlreadyExistsException();
  157. } catch(UsernameAlreadyExistsException uaee) {
  158. // redirect to result page
  159. response.sendRedirect("register-result.jsp");
  160. }
  161.  
  162. try {
  163. throw new SQLException();
  164. } catch(SQLException uaee) {
  165. response.sendRedirect("register-result.jsp");
  166. }
  167.  
  168. // wrong captcha answer
  169. } else {
  170.  
  171. user_type TINYINT UNSIGNED NOT NULL, /* VALUES: 0 - Guest, 1 - Admin, 2 - User */
  172.  
  173. 2 // 2 = User
  174.  
  175. enum UserType {GUEST, ADMIN, USER}
  176.  
  177. public boolean areElementsNull(String[] requiredUserDetails) {
  178. for(String detail : requiredUserDetails) {
  179. if(detail == null) {
  180. return true;
  181. }
  182. }
  183.  
  184. return false;
  185. }
  186.  
  187. String[] requiredUserDetails = {
  188. userId,
  189. userEmail1,
  190. userEmail2,
  191. userPassword1,
  192. userPassword2,
  193. captchaAnswer,
  194. };
  195.  
  196. RegistrationModule rm = new RegistrationModule();
  197. boolean areElementsNull = rm.areElementsNull(requiredUserDetails);
  198.  
  199. if(!areElementsNull) {
  200. // code here
  201.  
  202. } else {
  203. response.sendRedirect("index.jsp");
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement