javascript 使用 servlet 在登录页面本身显示密码错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27311256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display password error message in login page itself using servlet
提问by Premkumar
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">
在登录文件中
回答by Premkumar
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);
回答by Gopala Krishna Raju M
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.
不要忘记对字符串使用单引号。
回答by Braj
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. You can use <c:if>
for testing the error message.
使用 JSTL 和 EL 访问 JSP 中的请求属性。您可以<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. Not a good waybut 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.
如果您只想使用 HTML,请尝试使用。这不是一个好方法,但您可以为 login.html 和 login_error.html 创建两个单独的 HTML 页面,然后在出现任何错误时重定向到 login_error.html。
request.getRequestDispatcher("/login_error.html");