Java EE servlet page redirection + sending data to the controller
<p>I'm trying to make page redirection from Servlet but it doesn't work I don't know why, if anyone could help I'll be grateful, I spent 2 hours searching but I didn't get a result.</p>

<p>the JS code </p>

<pre><code>$("#login_form_submit").click(function(){
var form = $('#login_form');
    $.ajax({
            type: "Post",
            url: "AccountServlet",
            data: form.serialize(),
            dataType: "json",

            //if received a response from the server
            success: function( data, textStatus, jqXHR) {
                 if(data.success){
                    $("#ajaxResponse").html("");
                    console.log(data);

                    if (data.User.role == 1){
                        main.user = data.User; 
                        //window.location.href = "Student_home.jsp";
                    }
                 } 
                 //display error message
                 else {
                    $("#ajaxResponse").html("&lt;div&gt;&lt;b&gt;Something goes wrong check your email or password&lt;/b&gt;&lt;/div&gt;");

                 }
            },

            //If there was no response from the server
            error: function(jqXHR, textStatus, errorThrown){
                 $("#ajaxResponse").html("&lt;div&gt;&lt;b&gt;Something goes wrong check your email or password&lt;/b&gt;&lt;/div&gt;");
                 $("#ajaxResponse").html(jqXHR.responseText);
            },

            //capture the request before it was sent to server
            beforeSend: function(jqXHR, settings){
                //disable the button until we get the response
                $('#login_form_submit').attr("disabled", true);
            },

            //this is called after the response or error functions are finsihed
            //so that we can take some action
            complete: function(jqXHR, textStatus){
                //enable the button 
                $('#login_form_submit').attr("disabled", false);
            }

        });        

});
</code></pre>

<p>the servlet code </p>

<pre><code>@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    response.setHeader("Cache-control", "no-cache, no-store");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "-1");

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    response.setHeader("Access-Control-Max-Age", "86400");


    String userName = request.getParameter("userName");
    String password = request.getParameter("password");
    DatabaseWrapper context = new DatabaseWrapper();
    User user = context.Check_login(userName, password);
    session.current_user = user;
    Gson gson = new Gson(); 
    JsonObject myObj = new JsonObject();

    if (user == null){
        myObj.addProperty("success", false);
        out.println(myObj.toString());
    }else if (user instanceof Student){
        JsonElement userObject = gson.toJsonTree((Student)user);
        myObj.addProperty("success", true);
        myObj.add("User", userObject);
        out.println(myObj.toString());

        out.close();
        //response.setStatus(HttpServletResponse.SC_FOUND); 
        response.sendRedirect("Student_home.jsp");
        return;


    }
</code></pre>

<p>I tried also to use </p>

<pre><code>RequestDispatcher dispatcher = request.getRequestDispatcher("/Student_home.jsp");
        dispatcher.forward(request, response);
</code></pre>

<p>but it didn't succeeded, any suggestion !</p>

<p>only using javascript works fine with me, but I need redirect from the servlet!</p>

<p>I used this line in javascript </p>

<pre><code>window.location.href = "Student_home.jsp";
</code></pre>

<p>Guys I found the answer but for some reason I can't post it in solutions?!</p>

<p>the problem was I'm redirecting the ajax call and there is no way to prevent it, the idea is from the response in the jquery success I'll redirect using another call which create form element and submit it in natural way </p>

<p>my solution :</p>

<pre><code>function Redirect (servlet , method_type){
var form = $(document.createElement('form'));
$(form).attr("action", servlet);
$(form).attr("method", method_type);
$(form).css("display", "none");
form.appendTo( document.body );
$(form).submit();
</code></pre>

<p>};</p>

<p>it is done, thanks every one.</p>