<%-- processRegister.jsp Alan Pinder - April 10th 2005 Reads saved login information and either processes it, or sets up the error variable and shows the form output page Note: This page, as with all other processX pages, does NOT produce any output Classes Used: None Attributes Used: None Parameters Used: None --%> <%-- Standard page declarations --%> <%@ page import="apz.db.*" %> <%@ page session="true" %> <%@ page errorPage="error.jsp" %> <% // Create our validation object with default attributes FormValidation formValidation = new FormValidation("Registration Error", "register.jsp", "Please read the above messages and click here to re-register"); // Make sure the 3 main parameters are non-null // It's a fatal error if they are, so throw exception String userName = request.getParameter("userName"); String password = request.getParameter("password"); String password2 = request.getParameter("password2"); if (userName == null) throw new dbException("User name is null"); if (password == null) throw new dbException("Password is null"); if (password2 == null) throw new dbException("Retype is null"); // Now do further checking // DON'T throw exceptions as we want to handle the error gr*cefully // Tests we do: // Check username and password are not zero-length // Check user with that username exists // Check password matches // Witness our amazing form validation class at work! if (userName.length() == 0) formValidation.addErrorMessage("userName", "Please enter your preferred username"); if (userName.length() > dbUsers.MAX_USERNAME_LENGTH) formValidation.addErrorMessage("userNameLenMax", "Your preferred username is too long! Please choose a shorter one"); if (password.length() == 0) formValidation.addErrorMessage("password", "Please enter a password you want to use to protect your account"); if (!password.equals(password2)) formValidation.addErrorMessage("retype", "Your original and retype password values are not identical. Please enter the same word in both text fields!"); if (formValidation.validatedSuccess()) { // If we get this far it means all parameters are correct.. // Check the username is not already in use if (db.getUser(userName) != null) formValidation.addErrorMessage("userNameInUse", "The user name you have chosen is already being used by some-one else. Please choose a new one"); else { // All good so far.. now try and create the user! // It is likely that createUser() may throw an exception so we will need // to catch() it dbUser currentUser = db.createUser(userName, password); // Check we have recieved a valid reference // If not, throw exception if (currentUser == null) throw new dbException("Invalid user reference recieved"); // It's all good! Fill in user object with more parameters (if any) // Note: We don't need to check for NULL here (no NPE's will be throws currentUser.setValue("firstName", request.getParameter("firstName")); currentUser.setValue("surname", request.getParameter("surname")); // Log user in session.setAttribute("isActive", "True"); session.setAttribute("userName", userName); session.setAttribute("userObject", currentUser); } } // If operation was success forward to welcome page if(formValidation.validatedSuccess()) request.getRequestDispatcher("welcome.jsp").forward(request, response); else { // Otherwise fill in validation object and show error form session.setAttribute("formObject", formValidation); request.getRequestDispatcher("formOutput.jsp").forward(request, response); } %> <%-- No HTML code for 'processXXX' pages! --%> <%-- We should never arrive here!! --%>