I have a java servlet called LoginPage, which is the visual interface that the user will enter their login information into a form, and another servlet called Login, which is a temporary page of sorts that only does the checking, querying database to confirm it is indeed a valid login before redirecting to another servlet.
//LoginPage
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Login</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Activities Week Login</h1></br>");
out.println("<form action=\"login\" method=\"post\">"
+ "Email:</br>"
+ "<input type='text' name='Email'></br>"
+"Password:</br>"
+ "<input type='password' name='Password'></br></br>"
+ "<input type='submit' value='Submit'></form>");
out.println("</br></br></br>");
out.println("Dont have an account yet? <a href=\'createaccountpage\'>Create one.</a></br></br>");
out.println("Forgotten your password? <a href=\'forgottenpasswordpage\'>Reset it.</a>");
out.println("</body>");
out.println("</html>");
}
//Login
SQLConnection sqlconnection = new SQLConnection();
String Email = (String)request.getParameter("Email");
String Password = (String)request.getParameter("Password");
if(sqlconnection.checkValidLogin(Email, Password) == false)//If the login details are incorrect:
{
PrintWriter out = response.getWriter();
out.println("<meta http-equiv='refresh' content='3;URL='>");//redirects after 3 seconds
out.println("<p style='color:red;'>Login unsuccessful! Please check your details and try again.</p>");
response.sendRedirect("");
}
else//If the login details are correct:
{
User currentUser = sqlconnection.getCurrentUser(Email);
HttpSession session = request.getSession();
session.setAttribute("Email", Email);
if(sqlconnection.isUserTeacher(Email)==true)//If the user is a teacher
{
Teacher currentTeacher = new Teacher();
currentTeacher.setUser(currentUser);
session.setAttribute("currentTeacher", currentTeacher);
PrintWriter out = response.getWriter();
out.println("<meta http-equiv='refresh' content='3;URL=studenthomepage'>");//redirects after 3 seconds
out.println("<p style='color:green;'>Login successful!</p>");
response.sendRedirect("studenthome");
}
else{
Student currentStudent;
currentStudent = sqlconnection.getCurrentStudent(Email, currentUser);
session.setAttribute("currentStudent", currentStudent);
PrintWriter out = response.getWriter();
out.println("<meta http-equiv='refresh' content='3;URL=studenthomepage'>");//redirects after 3 seconds
out.println("<p style='color:green;'>Login successful!</p>");
response.sendRedirect("studenthome");
}
}
How can I display an error message that remains when I redirect back to LoginPage in the case that the login information provided is incorrect?
I have seen and tried some other methods (that did not help) such as what is already in the Login servlet:
out.println("<meta http-equiv='refresh' content='3;URL=studenthomepage'>");//redirects after 3 seconds
out.println("<p style='color:green;'>Login successful!</p>");
Is this method not a good solution (or am I just doing it wrong), if so what should I use?