ResponseText on Login Java Javascript



我想登录,失败时我想写在div上"错误的密码",但在成功时重定向到response.sendRedirect(".html"(;它只包括现有页面中请求的 HTML 页面。我怎么能做这样的事情?

xhr.onload = function (){
if (xhr.status === 200) {
document.getElementById('response').innerHTML = xhr.responseText; 
alert(password); 
} else if (xhr.status !== 200) { 
alert('Request failed. Returned status of ' + xhr.status); 
} 

问题是我正在对 ajax 请求执行此操作,并且我正在将失败的div 作为响应文本,但在成功时它不会重定向它只是在 html 中有一个 html

PreparedStatement ps=con.prepareStatement("select * from members where PatientEmail=? and PatientPassword=?");
ps.setString(1, email);
ps.setString(2, pass);
ResultSet rs=ps.executeQuery();
while (rs.next()) {
flag=1;
response.sendRedirect("InnerIndexRedesign.html");
//request.getRequestDispatcher("InnerIndexRedesign.html").forward(request, response);
}
out.println("Wrong username or password.");

实现此目的的一种方法是使用查询字符串。如果登录尝试不成功,您的 URL 可能看起来像http://localhost:8080/login?attempt=failure。然后在您的 HTML 页面中,您可以检查attempt的值,如果它等于failure,您可以显示一条消息。

对于重定向,您可以做同样的事情。评估另一个查询键的值,然后如果attempt的值等于success,您将重定向到该键的值,该键将存储重定向 URL。

如果你能提供你的代码,那就更容易回答了。无论如何,你可以做这样的事情:

网页部分

<p id="error"></p>

JS部分

var err = document.getElementById('error');
if(username-input == 'username' && userpassword-input == 'password'){
response.sendRedirect(".html");
} else {
err.innerHTML = 'your username and/or password is not correct';
}

最新更新