以下是jsp页面上的代码。这是一个用户登录页面..
<form onsubmit="return userValidation()" action="tempTestingPage.jsp" method="post">
<table>
<b><h3 style="text-align: center">Welcome back!</h3></b>
<h5 style="text-align: center">Sign in to continue to your account</h5>
<tr>
<td><label>User Name</label></td>
<td><input autofocus type="text" id="userloginid" name="LoginID" required="Enter your user name here"</td>
</tr>
<tr>
<td><br><label>Password</label></td>
<td><br><input type="text" id="userloginpassword" name="LoginPassword" required="Enter your Password here"</td>
</tr>
</table>
<div style="text-align: center;">
<br><input type="submit" style="width: 33%; border-radius: 10px; border-color: #ea0f1f;" value="Sign in">
</div>
<br><div id="registerlogin" style="text-align: center;">
Not Registered Yet? <a style="text-decoration: none; color: #8a2de3;" href="RegistrationForm.jsp" >Register Here</a>
</div>
</form>
</fieldset>
</div>
我已经编写了我的代码,以便在提交上述表单时,如果javascript函数返回true,则导航到另一个jsp页面,即tempTestingPage.jsp。否则,通过在javascript函数中显示其visibilty来显示以下错误。下面的所有javascript代码也都写在上面的jsp页面上。
<div id="invalidusernameorpassword" style="visibility: hidden">
<fieldset style="position: absolute; left: 37.4%; top: 7%; display: block; background-color: #fae4a8 ;width: 26%; height: 9.3%; border: 4px; border-color: #cd8686; border-style: solid;">
<table>
<tr>
<td><h4 style="font-family: sans-serif; text-align: left; margin-top: 0%; margin-bottom: 0%; color: #f40909; font-weight: bold;">There was a problem with your request.</h4></td>
</tr>
<tr>
<td><p style="font-family: sans-serif; text-align: left; font-size: 74%; margin-top: 0%; color: #f40909;">There was an error with your Username or Password combination. Please try again.</p></td>
</tr>
</table>
</fieldset>
</div>
这是我的javascript函数userValidation(),它在表单提交时调用: 在这段代码中,我向servlet UserPermissionServlet发送一个ajax请求,以确定输入的用户名和密码是否是正确的组合。如果正确组合,则返回“found”作为响应。否则返回“未找到”。无论响应是什么...... if条件总是失败并且else部分中的语句被执行。警报放在代码中仅供我参考。
function userValidation() {
var result = ajaxForPermissionInside("UserPermissionServlet?data1=" + document.getElementById("userloginid").value + "&data2=" + document.getElementById("userloginpassword").value);
alert(result);
if(result === "found"){
alert("In If Condition");
return true;
}
else{
alert("In else Condition");
document.getElementById("invalidusernameorpassword").style.visibility = "visible";
document.getElementById("userloginid").focus();
document.getElementById("userloginid").value = "";
document.getElementById("userloginpassword").value = "";
return false;
}
}
function ajaxForPermissionInside(url) {
var myrequest1 = new XMLHttpRequest();
myrequest1.open("POST", url, false);
myrequest1.send(null);
return(myrequest1.responseText);
}
servlet代码在这里:
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
if(userAccount== 1)
out.println("found");
else out.println("not found");
}
请帮我弄清楚我的javascript代码有什么问题。
答案 0 :(得分:1)
out.println()
在邮件后添加换行符。您将其与没有换行符的字符串进行比较。
改为使用out.print()
。