I have a servlet that processes user registration to a website. It takes inputs from an HTML form like username, password, email, etc.
MySQL Table:
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)
);
Servlet:
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");
}
}
Everything works as it should, however during a quick code review from my instructor, he commented I should not be catching NPEs. I am using the catch clause to redirect the user to index.jsp if they try to jump to the Servlet URL without going through the required pages. My other servlets are formatted similarly as well. What is the best practice if catching NPE is not encouraged?