如果使用 JavaScript 登录失败,如何在登录页面中显示消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11355854/
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
How to show a message in the login page if the login fails using JavaScript
提问by VBJ
I have developed a login page where the user id and minmum password length validation is done by JavaScript and I have a servlet that connects to DB and authenticates the user. Can someone please tell me how to display a message in the same login page when authentication fails?
我开发了一个登录页面,其中用户 id 和最小密码长度验证是由 JavaScript 完成的,我有一个连接到数据库并验证用户身份的 servlet。有人可以告诉我如何在身份验证失败时在同一登录页面中显示消息吗?
回答by Alex W
HTML (place this in the page's HTML where the error message should be displayed):
HTML(将其放在页面的 HTML 中应显示错误消息的位置):
<span id="login_failed"></span>
JavaScript:
JavaScript:
<script type="text/javascript">
var login_success = false; /* set this to true if the login was a success */
if(login_success == false)
{
document.getElementById("login_failed").innerHTML = "Login Failed.";
}
else
{
window.location = "http://www.yourpage.com/login_success.html";
}
</script>
回答by Muhammad Imran Tariq
Send a flag value of login failed from your servlet like this.
像这样从您的 servlet 发送登录失败的标志值。
request.setAttribute("loginResult", true);
Now get this value in JSP. Its default value will be false. if its true then show error message.
现在在 JSP 中获取这个值。其默认值为 false。如果为真,则显示错误消息。
<%
if(request.getAttribute("loginResult") != null && request.getAttribute("loginResult") == "true"){
%>
<p style="color:red"> Login Failed. Please try again. </p>
<%
}
%>
For Javascript call a function to show error, instead of <p style="color:red"> Login Failed. Please try again. </p>
from my above code.
对于 Javascript 调用一个函数来显示错误,而不是 <p style="color:red"> Login Failed. Please try again. </p>
我上面的代码。
回答by MaVRoSCy
in your web.xml put this
在你的 web.xml 把这个
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login.jsp?loginerror=100</form-error-page>
</form-login-config>
</login-config>
now in your login.jsp put this javascript to read the loginerror param and call it ondocument.ready (onload())
现在在您的 login.jsp 中放入这个 javascript 来读取 loginerror 参数并调用它 ondocument.ready (onload())
function gup()
{
var name = 'loginerror';
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
//here you don't have login errors
else
//here you have login error
}
enjoy
请享用
回答by Leap Bun
Print the JavaScript in Servlet.
在 Servlet 中打印 JavaScript。
The alert message:
System.out.println("<script>alert('Login fail.')</script>");
Redirect to login page again:
System.out.println("<script>window.location='loginUrl'</script>");
警报消息:
System.out.println("<script>alert('Login fail.')</script>");
再次重定向到登录页面:
System.out.println("<script>window.location='loginUrl'</script>");
回答by u283863
if(/*something fails*/){
alert("Login fails!");
}