我已经使用JSP(account.jsp)创建了一个登录页面,如果登录成功,servlet会重定向到home.jsp,如果notstays /重定向到account.jsp。
如果登录失败,我将无法显示错误消息。因为我创建了一个设置为hidden.i.e的错误div。 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);
}
答案 0 :(得分: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>