我认为有更好的方法可以做到这一点。 如果你知道告诉我。
我的登录页面包含用户名,密码和登录按钮。如果登录失败,我只想在输入密码下显示消息。
我的问题是该消息一直存在,直到我正确登录。 如果我关闭页面并再次打开,则会显示消息。现在我只想在刷新页面时清除变量,以便消息消失。
HTML:
<body>
<div class="container">
<div class="row">
<div class="wrap-login col-md-4 col-md-offset-4">
<h2 style="margin-bottom: 20px;">ATP Login</h2>
<form action="Login" method="post">
<label>Username</label>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="username" type="text" class="form-control" name="username" placeholder="Username">
</div>
<label style="margin-top: 15px;">Password</label>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="password" type="password" class="form-control" name="password" placeholder="Password">
</div>
<%
if(session.getAttribute("loginSuccessful") != null && session.getAttribute("loginSuccessful") == "false") {
%>
<div class="alert alert-danger" style="margin-top: 10px;">
<strong>Wrong username and password combination.</strong> Please, check your login information and try again.
</div>
<%
}
%>
<button type="submit" class="btn btn-default login-btn" >Login</button>
</form>
</div>
</div>
</div>
</body>
JAVA:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
//Check login
Boolean successful = CheckLogin(username,password);
if (successful) {
response.sendRedirect("Environments.jsp");
}
else {
session.setAttribute("loginSuccessful","false");
response.sendRedirect("index.jsp");
}
}
答案 0 :(得分:0)
不要将 loginSuccessful
放在session
范围内。将它放在request
范围内,因此它仅在处理当前登录请求时才有效。
抱歉,我没有看到您使用sendRedirect
。在这种情况下,您需要在if块中清除JSP页面上的session属性。只需将其设置为null
。