java.lang.illegal state exception


if (request.getParameter("print_type") != null) {
    String print_type = request.getParameter("print_type").toString();
    if (print_type.contains("1"))//Preprinted page
    {
        session.removeAttribute("default_selected_opd_ticket");
        session.setAttribute("default_selected_opd_ticket", "1");
        if (session.getAttribute("opdTicket_preprinted") != null) {
            printpg = session.getAttribute("opdTicket_preprinted").toString();
        } else if (print_type.contains("3"))//Preprinted page
        {
            session.removeAttribute("default_selected_opd_ticket");
            session.setAttribute("default_selected_opd_ticket", "3");
            printpg = "../opdreport/antenatal_report.jsp";
        }
    } else {  //for blank page
        session.removeAttribute("default_selected_opd_ticket");
        session.setAttribute("default_selected_opd_ticket", "2");
        if (session.getAttribute("opdTicket_blank") != null) {
            printpg = session.getAttribute("opdTicket_blank").toString();
        } else {
            printpg = "../opdreport/opdTicket_blank.jsp";
        }
    }
    //  printpg = "../opdreport/opdTicket_blank.jsp";//this is a jsp
    request.getSession().setAttribute("backpage", backpg);
    request.getSession().setAttribute("printpage", printpg);
    request.getSession().setAttribute("regNo", registration.getRegistrationno());
    request.getSession().setAttribute("pID", registration.getPatientid());
    registration = null;
    if (request.getParameter("SelectedOption") != null) {
        String strSelectedOption = request.getParameter("SelectedOption").toString();
        if (strSelectedOption.equals("withoutNk")) {
            response.sendRedirect("../opdreport/printframe.jsp");
            return;
        } else if (strSelectedOption.equals("withNK")) {
            response.sendRedirect("../opdregistration/Nk1Details.jsp");
            return;
        }
    }
}

上面给出了一部分代码。在这里,我无法重定向到提到的jsp页面。

所有数据都在保存,而不会重定向到页面,从而产生非法状态异常错误。在这里,我使用了三个值分别为 1、2、3 的单选按钮。

用户将根据要求选择单选按钮,并根据该按钮重定向到相应的页面。

很有可能

,您在执行sendRedirect()之前已经写入了响应流。如果您在刷新到输出流或写入 cookie 或标头等后尝试重定向,则会抛出IllegalStateException

最新更新