我的目标是在用户无法登录系统时创建错误消息。 我设法使用 session 创建错误消息。
但是,当我刷新浏览器时,错误消息仍然存在。它不应该出现。有没有办法让我在用户刷新浏览器时使错误消息不可见?
下面是我的代码。 帮助将不胜感激..谢谢! :)
在login.jsp
<div class="error">
<% String e = (String) session.getAttribute("error" );
if(e != null)
{
out.print(e);
}%>
</div>
在Servlet
HttpSession session = request.getSession(true);
session.setAttribute( "error", "Invalid username or password");
RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
dispatcher.forward( request, response);
最佳答案
session 属性在 HttpSession
的持续时间内有效。尝试使用请求属性
request.setAttribute( "error", "Invalid username or password");
仅在请求期间持续。
否则,您必须从 HttpSession
中删除该属性。
关于java - JSP和Servlet中用户使用Session登录失败时的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778342/