Java J2E不能在我的servlet中使用sendRedirect()

时间:2015-03-30 12:49:26

标签: java

我有一个错误,我似乎无法自行纠正

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    if (session.getAttribute("username") != null) {
        if ((boolean)session.getAttribute("usertype") == true) {
            int userID = Integer.parseInt(request.getParameter("id"));
            UserDAO dao = new UserDAO();
            User user = dao.ReturnUserID(userID);
            dao.DeleteUser(user);
            dao.Close();

            response.sendRedirect("/SupTracking/admin");
        }
        response.sendRedirect("/SupTracking/index");
    }
    response.sendRedirect("/SupTracking/index");
}

我在response.sendRedirect(“/ SupTracking / admin”)中收到错误,说我不能使用sendRedirect(),因为已经发送了响应。

错误应该非常简单,除了我不明白我在这个代码中发送了什么...

这是确切的堆栈跟踪:

java.lang.IllegalStateException: Impossible d''appeler "sendRedirect()" après que la réponse ait été envoyée
org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:494)
com.SupTracking.servlets.admindeluser.doGet(admindeluser.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我做错了什么?

3 个答案:

答案 0 :(得分:3)

每次重定向后添加return语句。否则,如果条件为真,则重定向多次,如果条件为真,则重定向。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    if (session.getAttribute("username") != null) {
        if ((boolean)session.getAttribute("usertype") == true) {
            int userID = Integer.parseInt(request.getParameter("id"));
            UserDAO dao = new UserDAO();
            User user = dao.ReturnUserID(userID);
            dao.DeleteUser(user);
            dao.Close();

            response.sendRedirect("/SupTracking/admin");
            return;
        }
        response.sendRedirect("/SupTracking/index");
        return;
    }
    response.sendRedirect("/SupTracking/index");
}

答案 1 :(得分:2)

您只能为响应对象调用sendRedirect一次。你应该改变你的逻辑,所以你知道它只会被调用一次:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    if (session.getAttribute("username") != null) {
        if ((boolean)session.getAttribute("usertype") == true) {
            int userID = Integer.parseInt(request.getParameter("id"));
            UserDAO dao = new UserDAO();
            User user = dao.ReturnUserID(userID);
            dao.DeleteUser(user);
            dao.Close();

            response.sendRedirect("/SupTracking/admin");
        }
        else
        {
            response.sendRedirect("/SupTracking/index");
        }
    }
    else
    {
        response.sendRedirect("/SupTracking/index");
    }
}

你的问题是:

 if ((boolean)session.getAttribute("usertype") == true) {
            int userID = Integer.parseInt(request.getParameter("id"));
            UserDAO dao = new UserDAO();
            User user = dao.ReturnUserID(userID);
            dao.DeleteUser(user);
            dao.Close();

            response.sendRedirect("/SupTracking/admin");******Called Here**********
        }
        response.sendRedirect("/SupTracking/index");*******Also Called Here**********

您无法多次重定向,响应只能重定向到一个页面。

答案 2 :(得分:1)

因为条件变为真,所以在响应对象上多次调用sendRedirect()。

试试这个:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    if (session.getAttribute("username") != null) {
        if ((boolean)session.getAttribute("usertype") == true) {
            int userID = Integer.parseInt(request.getParameter("id"));
            UserDAO dao = new UserDAO();
            User user = dao.ReturnUserID(userID);
            dao.DeleteUser(user);
            dao.Close();

         }
    }
    response.sendRedirect("/SupTracking/index");
}