Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE IF NOT EXISTS user(
- user_id VARCHAR(255),
- user_password VARCHAR(255) NOT NULL,
- user_last_name VARCHAR(255),
- user_first_name VARCHAR(255),
- user_email VARCHAR(255) UNIQUE NOT NULL,
- user_type TINYINT UNSIGNED NOT NULL, /* VALUES: 0 - Guest, 1 - Admin, 2 - User */
- PRIMARY KEY(user_id)
- );
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String userId = request.getParameter("username");
- String userFirstName = request.getParameter("firstname");
- String userLastName = request.getParameter("lastname");
- String userEmail1 = request.getParameter("email1");
- String userEmail2 = request.getParameter("email2");
- String userPassword1 = request.getParameter("pass1");
- String userPassword2 = request.getParameter("pass2");
- String captchaAnswer = request.getParameter("answer");
- try {
- // simple captcha
- HttpSession session = request.getSession(true);
- Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);
- request.setCharacterEncoding("UTF-8");
- boolean isCaptchaCorrect = captcha.isCorrect(captchaAnswer);
- session.setAttribute("isCaptchaCorrect", isCaptchaCorrect);
- session.setAttribute("userId", userId);
- session.setAttribute("userFirstName", userFirstName);
- session.setAttribute("userLastName", userLastName);
- session.setAttribute("userEmail1", userEmail1);
- session.setAttribute("userEmail2", userEmail2);
- if(isCaptchaCorrect) {
- // put database entries into a String[]
- DatabaseManipulator dm = new DatabaseManipulator();
- String[] usernameArray = dm.dbEntriesToArray("user_id");
- String[] emailArray = dm.dbEntriesToArray("user_email");
- // validate inputs
- RegistrationModule rm = new RegistrationModule();
- boolean hasDuplicateUsername = rm.hasDuplicate(usernameArray, userId);
- boolean hasDuplicateEmail = rm.hasDuplicate(emailArray, userEmail1);
- boolean isEmailMatch = rm.isMatch(userEmail1, userEmail2);
- boolean isPasswordMatch = rm.isMatch(userPassword1, userPassword2);
- // bind objects to session
- session.setAttribute("hasDuplicateUsername", hasDuplicateUsername);
- session.setAttribute("hasDuplicateEmail", hasDuplicateEmail);
- session.setAttribute("isEmailMatch", isEmailMatch);
- session.setAttribute("isPasswordMatch", isPasswordMatch);
- // throw user-defined exceptions
- if(hasDuplicateUsername) {
- try {
- throw new UsernameAlreadyExistsException();
- } catch(UsernameAlreadyExistsException uaee) {
- // redirect to result page
- response.sendRedirect("register-result.jsp");
- }
- } else if(hasDuplicateEmail) {
- try {
- throw new EmailAlreadyExistsException();
- } catch(EmailAlreadyExistsException eaee) {
- response.sendRedirect("register-result.jsp");
- }
- } else if(!isEmailMatch) {
- try {
- throw new MismatchedEmailsException();
- } catch(MismatchedEmailsException mee) {
- response.sendRedirect("register-result.jsp");
- }
- } else if(!isPasswordMatch) {
- try {
- throw new MismatchedPasswordsException();
- } catch(MismatchedPasswordsException mpe) {
- response.sendRedirect("register-result.jsp");
- }
- // register success
- } else {
- // assign if match
- String userPassword = userPassword1;
- String userEmail = userEmail1;
- // assemble user bean object
- User user = UserAssembler.getInstance(
- userId,
- userPassword,
- userLastName,
- userFirstName,
- userEmail,
- 2 // 2 = User
- );
- // insert user into database
- dm.registerUser(user);
- response.sendRedirect("register-result.jsp");
- }
- // wrong captcha answer
- } else {
- response.sendRedirect("register-result.jsp");
- }
- } catch(NullPointerException npe) {
- // redirect when servlet is illegally accessed
- response.sendRedirect("index.jsp");
- }
- }
- try {
- throw new SomeException();
- } catch (SomeException uaee) {
- response.sendRedirect("some-result.jsp");
- }
- response.sendRedirect("some-result.jsp");
- if(hasDuplicateUsername) {
- response.sendRedirect("register-result.jsp");
- } else if(hasDuplicateEmail) {
- response.sendRedirect("register-result.jsp");
- } else if(!isEmailMatch) {
- response.sendRedirect("register-result.jsp");
- } else if(!isPasswordMatch) {
- response.sendRedirect("register-result.jsp");
- }
- String userId = request.getParameter("username");
- String userFirstName = request.getParameter("firstname");
- String userLastName = request.getParameter("lastname");
- String userEmail1 = request.getParameter("email1");
- String userEmail2 = request.getParameter("email2");
- String userPassword1 = request.getParameter("pass1");
- String userPassword2 = request.getParameter("pass2");
- String captchaAnswer = request.getParameter("answer");
- if (userId == null || userFirstName == null || userLastName == null ||
- userEmail1 == null || ...) {
- response.sendRedirect("index.jsp");
- return;
- }
- try {
- throw new UsernameAlreadyExistsException();
- } catch(UsernameAlreadyExistsException uaee) {
- // redirect to result page
- response.sendRedirect("register-result.jsp");
- }
- try {
- throw new SQLException();
- } catch(SQLException uaee) {
- response.sendRedirect("register-result.jsp");
- }
- // wrong captcha answer
- } else {
- user_type TINYINT UNSIGNED NOT NULL, /* VALUES: 0 - Guest, 1 - Admin, 2 - User */
- 2 // 2 = User
- enum UserType {GUEST, ADMIN, USER}
- public boolean areElementsNull(String[] requiredUserDetails) {
- for(String detail : requiredUserDetails) {
- if(detail == null) {
- return true;
- }
- }
- return false;
- }
- String[] requiredUserDetails = {
- userId,
- userEmail1,
- userEmail2,
- userPassword1,
- userPassword2,
- captchaAnswer,
- };
- RegistrationModule rm = new RegistrationModule();
- boolean areElementsNull = rm.areElementsNull(requiredUserDetails);
- if(!areElementsNull) {
- // code here
- } else {
- response.sendRedirect("index.jsp");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement