AJAX响应冲突

I have a problem with my ajax redirection on response. The redirection works perfectly, but when, later, I have to return a Boolean with response, it returns the redirection.

Here is the code. The concerned lines have comments :

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Worker extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static String firstName = "";
    private static String lastName = "";
    private static boolean doAnimWheel = false;
    private static String portion;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // begin recovering form
        Worker.firstName = request.getParameter("firstName");
        Worker.lastName = request.getParameter("lastName");

        response.sendRedirect("launch.html"); // TODO find why it blocks response
        // end recovering form

        String param = request.getParameter("srcId");
        if(param != null) {
            if(param.equals("launch")) {
                Worker.doAnimWheel = new Boolean(request.getParameter("doAnimWheel")).booleanValue();
                return;
            }
            else if(param.equals("wheel")) {
                response.setContentType("text/plain");
                PrintWriter out = response.getWriter();
                out.print(Worker.doAnimWheel); // Here I have to return my Boolean, but it return launch.html
                out.flush();
                out.close();
                return;
            }
            else if(param.equals("result")) {
                Worker.portion = request.getParameter("portion");
                Worker.doAnimWheel = new Boolean(request.getParameter("doAnimWheel")).booleanValue();
                return;
            }
        }
    }
}

I think the problem is that you always send the redirect at the beginning of your method

    response.sendRedirect("launch.html"); // TODO find why it blocks response
    // end recovering form

Java documentation for the sendRedirect method HttpServletResponse states: "After using this method, the response should be considered to be committed and should not be written to."
What you try to return later is evidently ignored.


You may want to move the sendRedirect calling to the branches of code that actually need to perform the redirect, like this:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // begin recovering form
        Worker.firstName = request.getParameter("firstName");
        Worker.lastName = request.getParameter("lastName");

        String param = request.getParameter("srcId");

        if(param.equals("launch")) {
                        Worker.doAnimWheel = new Boolean(request.getParameter("doAnimWheel")).booleanValue();
                        response.sendRedirect("launch.html");
                        return;
                    }
                    else if(param.equals("wheel")) {
                        response.setContentType("text/plain");
                        PrintWriter out = response.getWriter();
                        out.print(Worker.doAnimWheel); // Here I have to return my Boolean, but it return launch.html
                        out.flush();
                        out.close();
                        return;
                    }
                    else if(param.equals("result")) {
                        Worker.portion = request.getParameter("portion");
                        Worker.doAnimWheel = new Boolean(request.getParameter("doAnimWheel")).booleanValue();
                        response.sendRedirect("launch.html");
                        return;
                    }
        }
    }
}