0

I've created a login form as

<div class="top1">
    <span id="book_store">Book Store</span>
    <div class="login_signup_form">
        <input type="email" id="email_id" placeholder="Email" name="email" />
        <input type="password" id="pas" placeholder="Password" name="password" />
        <span id="wrongPass" style="font-size: 15px; color: red; display: none;">
            Wrong Password
        </span>
        <button id="logIn">Log In</button>
        <button id="signUp">Sign Up</button>
    </div>
</div>

I am validating the email and password using ajax which is absolutely fine.

$("#logIn").click(function(){
   var info = {
       email: $("#email_id").val(),
       password: $("#pas").val()
   } ;
   $.ajax({
       type: 'POST',
       url: 'login',
       data: info,
       success: function (data) {
        if(data==="wrongPassword")
            $("#wrongPass").show();

       },
       error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus);
       }
   });
});

When password is wrong then error is shown on the same page but i want the Servlet to redirect the request to JSP page if password is correct. Servlet code is :

if(!pass.equals(tempPass))
{
    writer.print("wrongPassword");
}
else
{
   dispatcher =   request.getRequestDispatcher("login.jsp");
   dispatcher.forward(request,response);
}

wrong password case works fine but when password is correct then request is not redirected to login.jsp. I think it returns to ajax call. Can i break the ajax call return in else condition in servlet so that request is not returned to ajax call instead it is forwarded to jsp page??? PLZ help!!!!

It's not a duplicate question...

I want to redirect request from servlet to jsp. I've sent request to that servlet using ajax and trying to redirect the request to login.jsp in else block but it is not being directed...Same page remains in the window.

Dhruvam Gupta
  • 482
  • 5
  • 10
  • Unless there is some extra JavaScript you haven't shown, there is nothing in here that can make it redirect at the client end. Simply redirecting from the servlet won't actually do anything to the client, as the response is processed by JavaScript. – user1751825 Feb 21 '16 at 14:35
  • I gather you didn't understood the below answer and the one in duplicate? Just do `window.location='newurl'` in JavaScript. You just have the servlet pass that `newurl` as ajax response. – BalusC Feb 21 '16 at 17:29

1 Answers1

1

The servlet is processed by the server, the javascript, by the browser. I'm sure you know this, but possibly you're still getting them a bit confused.

Your servlet receives requests, and sends responses. This is all it can do.

What you could do is instead of showing an alert, get the javascript to redirect to the appropriate page, like so...

location.href = "login.jsp"

Having said that, I think even this is the wrong approach. You probably shouldn't be using ajax if you want to redirect them to a different page on failed login. You generally use ajax when you want to avoid full page refresh.

You might be better off simply posting directly to the servlet from the form, and leave ajax out of it altogether.

user1751825
  • 4,029
  • 1
  • 28
  • 58
  • but i don't want refresh page when wrong password is entered....This is reason why i'm using ajax but at the same time it creates problem for redirecting the request to jsp.....So i used the same location.href to request to login.jsp after successful return from servlet – Dhruvam Gupta Feb 21 '16 at 12:12
  • Ah, I think I know what you mean. You mean you want the servlet to transfer execution to login.jsp and return that content to ajax? Have you tried simply issuing a redirect from within the servlet. I believe the ajax calls, by default will follow redirects. – user1751825 Feb 21 '16 at 12:18
  • yes i want the same but servlet is not directing the request to login.jsp . I tried it in else block @user1751825 – Dhruvam Gupta Feb 21 '16 at 13:39
  • Best to try to isolate the problem. Before attempting to call the servlet from ajax, try calling it from curl, checking the response headers, to make sure it's actually responding with the correct HTTP redirection code. – user1751825 Feb 21 '16 at 14:29
  • Actually first step would be to use firebug to check if the correct login content is being returned to ajax. – user1751825 Feb 21 '16 at 14:32