[英]Display password error message in login page itself using servlet
I want to display Username or password incorrect message in a particular <div>
in the Login page.我想在登录页面的特定
<div>
中显示用户名或密码错误消息。
Here is my login.html code.这是我的 login.html 代码。
<div class="wrapper">
<form class="form-signin" method="POST" action="j_security_check">
<h2 class="form-signin-heading text-center" id="head">Login</h2>
<input type="text" class="form-control" id="txtuser" name="j_username" placeholder="Username" required="required" />
<input type="password" class="form-control" id="txtuser" name="j_password" placeholder="Password" required="required"/>
<div id="error"></div>
<button class="btn btn-lg btn-default btn-block login1" id="txtuser" type="submit"><span class="glyphicon glyphicon-log-in"></span> Login</button>
</form>
</div>
Here is my servlet code这是我的 servlet 代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
RequestDispatcher rd=request.getRequestDispatcher("/login.html");
out.println("<script>");
out.println("document.getElementById('error').innerText=Sorry UserName or Password Error");
out.println("</script>");
rd.include(request, response);
}
The message is not displaying in the <div id="error">
in login file该消息未显示在登录文件的
<div id="error">
中
I found an answer.It is working我找到了答案。它正在工作
Here is my JSP这是我的 JSP
<%
String login_msg=(String)request.getAttribute("error");
if(login_msg!=null)
out.println("<font color=red size=4px>"+login_msg+"</font>");
%>
Here is my Servlet这是我的 Servlet
request.setAttribute("error","Invalid Username or Password");
RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");
rd.include(request, response);
Just set the error message in request as an attribute and show the error div if it's not null.只需将请求中的错误消息设置为属性,如果不为空则显示错误 div。
Servlet:小服务程序:
request.setAttribute("error","Invalid Username/password");
Login.jsp:登录.jsp:
Use JSTL and EL for accessing request attribute in JSP.使用 JSTL 和 EL 访问 JSP 中的请求属性。 You can use
<c:if>
for testing the error message.您可以使用
<c:if>
来测试错误消息。
<c:if test="${not empty requestScope.error}">
<!-- Show the error div with message-->
</c:if>
Use Implicit object requestScope
to get the attribute from request scope.使用隐式对象
requestScope
从请求范围获取属性。
If you want to use HTML only then try it.如果您只想使用 HTML,请尝试一下。 Not a good way but you can create two separate HTML pages for login.html and login_error.html then redirect to login_error.html when there is any error.
这不是一个好方法,但您可以为 login.html 和 login_error.html 创建两个单独的 HTML 页面,然后在出现任何错误时重定向到 login_error.html。
request.getRequestDispatcher("/login_error.html");
Try this:尝试这个:
request.getRequestDispatcher("signin.html").include(request, response);
out.println("<script>document.getElementById('error').innerHTML='Sorry UserName or Password'; </script>");
Don't forget to use single-quotes for string.不要忘记对字符串使用单引号。
Simply move只需移动
rd.include(request, response);
before your print statements and it should work.在您的打印语句之前,它应该可以工作。 It is because you want the script to execute after the div is defined.
这是因为您希望脚本在定义 div 后执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.