0

I have a problem with calling ajax requests.the Servlet returns whole page as response

HTML Code

<form action="userlogin" method="POST">
            User name : <input type="text" name="username" id="username"/><br/>
            Password : <input type="text" name="password" id="password"/><br/>
            <input type="submit" value="Login" name="submitbtn" id="submitbtn"/><br/>
            <label id="failedText"></label>
</form>

JQuery Code

$(document).ready(function () {
$("input[name='submitbtn']").click(function (e) {
    e.preventDefault();
    $.ajax({
        url: 'userlogin',
        type: 'POST',
        data: $("form[action='userlogin']").serializeArray(),
        success: function (responseText) {
            if (responseText != 'failure')
                $("form[action='userlogin']").submit();
            else
                $('#failedText').text('Invalid Username or password');
        }
    });

});
});

Serlet Code

String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
if (username.equals("admin") && password.equals("password")) {
    path = "adminhome.html";
    response.sendRedirect(path);
}else
    response.getWriter().write("failure");

if it is failure it works the way i wanted, if it success i need to navigate to admin home page,but the code responses with the whole page as response text.

Nabeel
  • 455
  • 3
  • 14

1 Answers1

1

When you do a response.sendRedirect(path), the browser would again make a call to load the new path. In your case the first request to userlogin is a AJAX call and $.ajax would follow the redirects and fetch the final html to be rendered and hence you are getting that result. Instead of doing a sendRedirect, you can return the path as a string and load the content by using JS.

Update the Servlet code for admin user check

if (username.equals("admin") && password.equals("password")) {
    path = "adminhome.html";
    response.getWriter().write(path);
}

and update the JS to

success: function (responseText) {
    if (responseText != 'failure')
        // make sure you provide the correct URL from the server
        window.location = responseText;
    else
        $('#failedText').text('Invalid Username or password');
}
GSSwain
  • 5,787
  • 2
  • 19
  • 24
  • Is there any security related problem using your method?,And thanks for the answer. – Nabeel Mar 11 '18 at 10:50
  • I assume, you are doing this for learning. Of course you would not be hard coding the credentials on a Servlet to check for admin user. All other resources, which should only allow an admin user, must check if the user has the right authorization and then allow the user. If you are doing a redirect anyways, why use Ajax. Simply redirect to the correct page based on the user. – GSSwain Mar 11 '18 at 11:07
  • yes this is for my project, not using hard coded credentials, its used for checking the code, and separate servlet for admin, how can i interact with user friendly way without using ajax? – Nabeel Mar 11 '18 at 13:31