我的目标是在用户无法登录到系统时创建错误消息。我成功地使用会话创建了一个错误消息。
但是,当我刷新浏览器时,错误消息仍然存在。它不应该出现。当用户刷新浏览器时,是否有一种使错误消息不可见的方法?
下面是我的密码。帮助是appreciate..Thanks!:)
在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); 发布于 2013-12-26 02:03:30
会话属性在HttpSession的持续时间内活动。尝试使用请求属性
request.setAttribute( "error", "Invalid username or password");它只在请求的持续时间内持续。
否则,您将不得不从HttpSession中删除该属性。
发布于 2013-12-26 03:16:29
您可以使用请求对象,也可以检查会话属性是否存在于会话中,如果存在,只需删除它。
即jsp
<% String e = (String) session.getAttribute("error" );
if(e != null)
{
out.print(e);
}else{
out.print("All Is Well");
}>servlet
HttpSession session = req.getSession(true);
try{
if(null != (session.getAttribute("error"))){
session.removeAttribute("error");
}else{
session.setAttribute( "error", "Invalid username or password");
}
}catch(Exception er){
er.printStackTrace();
}
RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/LoginTest.jsp");
dispatcher.forward( req, res); https://stackoverflow.com/questions/20778342
复制相似问题