Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

response .sendRedirect not working

Tags:

jsp

servlets

I have an application where i have to redirect from a login page to another page the user name and the password i enter in the login page is going to another servlet.so from the servlet if i found the login values are matched i will redirect to another page,But this things are not working....I am posting my code

  boolean validationFlag = qb.userValidation(loginVO);
        if (validationFlag) {

            //This is for Ajax return 

            System.out.println("we are here");
            response.sendRedirect("../main.jsp");
            System.out.println("we are back");
        }
    } catch (Exception e) {

        e.printStackTrace();
    }
} 

here validationFlag is true and i have to redirect to anther page but it is not working

like image 865
Subho Avatar asked Nov 30 '25 04:11

Subho


2 Answers

You have to return afterwards. Otherwise, execution keeps going until the end of the JSP/servlet.

response.sendRedirect("../main.jsp");
return;

So without the return, your System.out.println("we are back"); will be executed.

like image 123
developerwjk Avatar answered Dec 02 '25 17:12

developerwjk


First of all get clear with concept of when to use sendRedirect and when to use RequestDispatcher. (you are using 2 step process for simple validation)

http://www.beingjavaguys.com/2013/05/difference-between-request-dispatcher.html http://www.coderanch.com/t/540671/Servlets/java/difference-RequestDispatcher-sendRedirect

remove those println statements. Remember you can't do sendRedirect() after writing to response object.

And now try,

RequestDispatcher view = request.getRequestDispatcher("main.jsp");
view.forward(request, response);

here there is no problem of relative urls like the one you were using.

Hope this helps....happy coding

like image 35
Swapnil Sawant Avatar answered Dec 02 '25 17:12

Swapnil Sawant