简体   繁体   中英

ResponseText on Login Java Javascript

I want to make a Login and on failure I want to write on a div "wrong password" but on success redirect on response.sendRedirect(".html "); it just includes the html page requested withing the existing one . How can I do such a thing ?

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); 
} 

The thing is I am doing this on ajax requests and I m giving as a responsetext the div for failure but in success it doesn't redirect it just goes having an html withing an 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.");

One way you could accomplish this is by using query strings. If a login attempt is unsuccessful, your url may look like http://localhost:8080/login?attempt=failure . Then in your HTML page, you could check for the value of attempt and if it equals failure , you could display a message.

For the redirect, you could do the same thing. Evaluate for the value of another query key, then if the value of attempt equals success , you would redirect to the value of that key which would store the redirect url.

If you could provide your code, it would be easier to answer. Anyways, you could do something like this:

HTML part

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

JS part

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';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM