I have an HTML login form that is processed by a servlet, which then redirects the user to a result page.
login.jsp:
<strong>LOG IN</strong>
<p>Please enter your credentials.</p>
<form action="process-login.jsp" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" size="30" required title="Please enter your username."/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" size="30" required title="Please enter your password."/></td>
</tr>
</table>
<br/>
<input type="submit" value="Login"/>
</form>
Servlet (process-login.jsp):
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userId = request.getParameter("username");
String userPassword = request.getParameter("password");
if(userId != null && userPassword != null) {
DatabaseManipulator dm = new DatabaseManipulator();
String[] usernameArray = dm.dbEntriesToArray("user_id");
String registeredPassword = dm.getRegisteredPassword(userId);
LoginModule lm = new LoginModule();
boolean hasExistingAccount = lm.hasExistingAccount(usernameArray, userId);
boolean isPasswordCorrect = lm.isPasswordCorrect(userPassword, registeredPassword);
// returns new session if it does not exist
HttpSession session = request.getSession(true);
// bind objects to session
session.setAttribute("hasExistingAccount", hasExistingAccount);
session.setAttribute("isPasswordCorrect", isPasswordCorrect);
if(!hasExistingAccount) {
response.sendRedirect("login-result.jsp");
} else if(!isPasswordCorrect) {
response.sendRedirect("login-result.jsp");
// login success
} else {
String currentUserId = dm.getCurrentUserId(userId);
int currentUserType = dm.getCurrentUserType(userId);
session.setAttribute("currentUserId", currentUserId);
session.setAttribute("currentUserType", currentUserType);
response.sendRedirect("index.jsp");
}
} else {
// redirect on illegal access of servlet
response.sendRedirect("index.jsp");
}
}
login-result.jsp:
<strong>LOGIN RESULT</strong>
<%
// retrieve objects from session
Boolean hasExistingAccount = session.getAttribute("hasExistingAccount") != null ? (Boolean) session.getAttribute("hasExistingAccount") : true;
Boolean isPasswordCorrect = session.getAttribute("isPasswordCorrect") != null ? (Boolean) session.getAttribute("isPasswordCorrect") : true;
if(!hasExistingAccount) {
%>
<form action="login.jsp">
<table>
<tr><td style="color: #FF0000">User does not exist!</td></tr>
</table>
<br/>
<input type="submit" value="Try Again"/>
</form>
<% } else if(!isPasswordCorrect) { %>
<form action="login.jsp">
<table>
<tr><td style="color: #FF0000">Username or password is incorrect!</td></tr>
</table>
<br/>
<input type="submit" value="Try Again"/>
</form>
<% } else {
// redirect on illegal access of jsp
response.sendRedirect("index.jsp");
}
%>
(Kindly forgive the scriptlets for now, as it is what we have reached in our lessons so far, though I am now aware it is deprecated.)
I use plain if-else statements, where the result page gives the appropriate message. Is this an acceptable way to handle user input errors?