1

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?

1 Answers1

0

You can set attribute with some message like below :

 String msg=  "Sorry !! You have an error";
            request.getSession().setAttribute("msg", msg);//setting attribute
            // forwards message to your page
             request.getRequestDispatcher("yourloginpage").forward(request,response);

Now,on your login page write like below at top of your form :

     HttpSession session = request.getSession();//getting session
     String msg;
   //checking if msg is not null 
      if(session.getAttribute("msg")!=null){
         msg= (String)session.getAttribute("msg");
        out.println("<p style='color:green;'> "+msg+"</p>"); //printing message
       }

Hope this helps you !

Swati
  • 28,069
  • 4
  • 21
  • 41
  • Thanks! In the first block of code in your answer, is there any particular reason you choose to use `request.getRequestDispatcher("yourloginpage").forward(request,response);` instead of `response.sendRedirect("yourloginpage")`? – V. Sanguinar Apr 18 '19 at 15:34
  • here read [this](https://stackoverflow.com/questions/20371220/what-is-the-difference-between-response-sendredirect-and-request-getrequestdis) . – Swati Apr 18 '19 at 15:41
  • have done. I am still unsure when to use which, is it advisable/better practice to use `forward(request,response)` in most cases then? – V. Sanguinar Apr 18 '19 at 15:53
  • Read [this](https://stackoverflow.com/questions/6068891/difference-between-jsp-forward-and-redirect/6068925#6068925) also , here examples are given , what to use when .. it will help you :) – Swati Apr 18 '19 at 16:04
  • I see... so by using `forward` the page viewed by the user does not change, but the request has been passed to another servlet on the server, thus allowing the error to be printed. I think I get it now? thanks a bunch :) – V. Sanguinar Apr 18 '19 at 16:13
  • In this case though, I cant see the difference between using forward and sendredirect as the session attribute `msg` has been added and the if statement on `loginpage` will check for it regardless? – V. Sanguinar Apr 18 '19 at 16:27
  • Yes , you can use `sendRedirect` also , if you want – Swati Apr 18 '19 at 16:31