AJAX响应冲突

时间:2012-07-25 07:07:12

标签: ajax redirect response

我在响应时遇到了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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为问题是你总是在方法的开头发送重定向

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

sendRedirect方法的Java文档HttpServletResponse声明:“使用此方法后,应该认为响应已提交,不应写入。” 你后来试图返回的内容显然被忽略了。


您可能希望将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;
                    }
        }
    }
}