0

我的 ajax 响应重定向有问题。重定向工作得很好,但是当我必须返回一个带有响应的布尔值时,它会返回重定向。

这是代码。有关行有评论:

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;
            }
        }
    }
}
4

1 回答 1

0

我认为问题在于您总是在方法开始时发送重定向

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

sendRedirect 方法 HttpServletResponse 的 Java 文档指出:“使用此方法后,应将响应视为已提交且不应写入。”
您稍后尝试返回的内容显然被忽略了。


您可能希望将 sendRedirect 调用移动到实际需要执行重定向的代码分支,如下所示:

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;
                    }
        }
    }
}
于 2012-07-25T07:37:22.350 回答