我尝试了此操作,但不显示消息仅重定向 login.jsp
<form method="post" action="Login_Servlet" > <input name="idUsuario" type="text"/> <input name="password" type="password" /> <button type="submit">Entrar</button> </form>
Login_Servlet
response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String userid= request.getParameter("idUser"); String password = request.getParameter("password"); Login_Service login_Service = new Login_Service(); boolean result = login_Servicio.aut(userid, password); Usuario user = login_Servicio.getUsuariosByUsuario(userid); if(result == true){ request.getSession().setAttribute("user", user); response.sendRedirect("vistas/Inicio.jsp"); } else{ out.println("<script type=\"text/javascript\">"); out.println("alert('User or password incorrect');"); out.println("</script>"); response.sendRedirect("index.jsp"); }
是否可以显示这样的消息? 如果是这样,我做错了吗?
您可能会这样做:
else { out.println("<script type=\"text/javascript\">"); out.println("alert('User or password incorrect');"); out.println("location='index.jsp';"); out.println("</script>"); }
编辑: 2018年3月20 日,印度标准时间08:19
或 不使用js
else { out.println("<meta http-equiv='refresh' content='3;URL=index.jsp'>");//redirects after 3 seconds out.println("<p style='color:red;'>User or password incorrect!</p>"); }