问题描述
如果登录成功,那么我将使用JSP(account.jsp)创建一个登录页面,如果不进行,则servlet重定向到home.jsp。
如果登录失败,我将不会显示错误消息。
为此,我创建了一个错误div,将其设置为hidden.ie style="display:none;"
每当登录出错时,我都希望将其显示设置更改为显示,即“ style =“ display:block;”
我想从servlet做到这一点。 有可能吗? 如果是,怎么办?
登录表单
<div class="col-sm-4">
<h3><STRONG>Login</STRONG></h3>
<div id="login_alert" class="alert alert-danger" style="display:none;">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Unsuccessful!</strong> Invalid Input
</div>
<form role="form" action="login" method="post">
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="pwd" name="pwd" placeholder="Enter Password">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button id ="submit" type="submit" class="btn btn-success">Submit</button>
</form>
</div>
Servlet代码
if(user exists){
HttpSession session = request.getSession();
session.setAttribute("user",user);
session.setAttribute("pwd",pwd);
request.getRequestDispatcher("./home.jsp").forward(request, response);
}else{
//document.getElementById('login_alert').style.display='block';
request.getRequestDispatcher("./account.jsp").forward(request, response);
}
1楼
1-添加请求属性
else{
request.setAttribute("loginFailed",true);
request.getRequestDispatcher("./account.jsp").forward(request, response);
}
2-有条件地在JSP中使用它
<div id="login_alert" class="alert alert-danger"
style="display:${loginFailed ? 'block' : 'none'}" >
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Unsuccessful!</strong> Invalid Input
</div>