简体   繁体   English

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

[英]How can I display an error message, that stays when redirected back to a different servlet?

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的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");
            }
        }

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? 如果提供的登录信息不正确,如何显示重定向到LoginPage时仍然存在的错误消息?

I have seen and tried some other methods (that did not help) such as what is already in the Login servlet: 我已经看到并尝试了其他方法(没有帮助),例如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? 这种方法不是一个好的解决方案(或者我只是做错了),如果可以的话,该怎么用?

You can set attribute with some message like below : 您可以通过以下message set attribute

 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 : 现在,在您的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
       }

Hope this helps you ! 希望这对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM