如何显示错误消息,该错误消息在重定向回另一个servlet时仍然存在?

时间:2019-04-18 14:02:04

标签: java servlets

我有一个名为LoginPage的Java servlet,这是用户将其登录信息输入到表单中的可视界面,还有另一个名为Login的servlet,这是一种临时页面,仅执行检查,查询数据库到在重定向到另一个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");
            }
        }

如果提供的登录信息不正确,如何显示重定向到LoginPage时仍然存在的错误消息?

我已经看到并尝试了其他方法(没有帮助),例如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>");

此方法不是很好的解决方案(或者我只是做错了),如果可以的话,该怎么用?

1 个答案:

答案 0 :(得分:1)

您可以set attribute和一些message如下所示:

 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);

现在,在您的login page上,在表格顶部的下方写下:

     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
       }

希望这对您有帮助!