如何从servlet在jsp中显示警报然后重定向到另一个jsp? [英] How to show alert in a jsp from a servlet and then redirect to another jsp?
本文介绍了如何从servlet在jsp中显示警报然后重定向到另一个jsp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过了,但不显示消息仅重定向登录.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 月 20th IST 08:19
Edited: 20th March 2018, 08:19 IST
OR 不使用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>");
}
这篇关于如何从servlet在jsp中显示警报然后重定向到另一个jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文