繁体   English   中英

用户在JSP和Servlet中无法使用Session登录时出现错误消息

[英]Error message when user Fails to Login using Session in JSP and Servlet

我的目标是在用户无法登录系统时创建一条错误消息。 我设法使用会话创建错误消息。

但是,当我刷新浏览器时,错误消息仍然存在。 它不应该出现。 当用户刷新浏览器时,我是否有办法使错误消息不可见?

以下是我的代码。 帮助将不胜感激..谢谢! :)

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); 

会话属性在HttpSession 尝试使用请求属性

request.setAttribute( "error", "Invalid username or password");

仅在请求期间持续。

否则,您将必须从HttpSession删除该属性。

您可以使用请求对象,也可以仅检查会话中是否存在session属性,如果存在,则可以将其删除。

即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); 

暂无
暂无

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

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