Servlet with java.lang.IllegalStateException:在提交响应后无法转发

时间:2014-03-30 12:06:17

标签: java servlets httprequest httpresponse

在我的servlet中,我有这段代码:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    Utils.CheckSession(request,response);

    String op = request.getParameter("op");
    int DaysToAdd = request.getParameter("daysToAdd") != null ? Integer.valueOf(request.getParameter("daysToAdd")) :  0;

    ArrayList<Giorno> giorni = new ArrayList<Giorno>();

    /*HERE I FILL ArrayList giorni and calculate other variables*/


    if ("cal".equals(op))
    {
        request.setAttribute("giorni", giorni);
        request.setAttribute("daysToAdd", DaysToAdd);
        request.getRequestDispatcher("GestioneCalendario.jsp").forward(request, response);

    }
    else if("utente".equals(op))
    {
        // ricavare abbonamento dell'utente
        String idu = (String) request.getAttribute("idu");
        Abbonamento a = null;
        int iDa = Utils.getIdAbbonamentoAttivoUtente(idu);
        a = Utils.getAbbonamentoFromId(iDa);
        request.setAttribute("abbonamento", a);
        request.getRequestDispatcher("Lezioni.jsp").forward(request, response);

    }
    else
    {
        request.setAttribute("giorni", giorni);
        request.setAttribute("daysToAdd", DaysToAdd);       
        request.getRequestDispatcher("GestioneLezioniNuovoLayout.jsp").forward(request, response);

    }

}

问题可能在于方法CheckSession?

public static void CheckSession(HttpServletRequest request,
        HttpServletResponse response) {
    // TODO Auto-generated method stub
     HttpSession session = request.getSession(true);
     String logged = null;
     if (session!=null)
          logged = (String) session.getAttribute("logined");
     if(logged == null)
     {
         request.setAttribute("errore", "Devi loggarti!");
        try {
            request.getRequestDispatcher("Admin.jsp")
            .forward(request, response);
            return;
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }

但它给了我这个例外:

GRAVE: Servlet.service() for servlet [TakeDates] in context with path [/Spinning] threw     exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:349)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at servlet.TakeDates.doGet(TakeDates.java:368)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

使用此URL调用servlet

http://localhost:8080/Spinning/TakeDates?op=cal

任何人都可以帮助我吗?提前谢谢!

2 个答案:

答案 0 :(得分:4)

在代码中的某处,您已对响应对象提交了一些响应。

在向jsp提交任何输出之前,您必须先发送到response object页面。

来自Documentation

  应该在响应提交之前调用

forward   客户端(在刷新响应正文输出之前)。如果回应   已经提交,这个方法抛出了   IllegalStateException异常。响应缓冲区中未提交的输出是   在前锋之前自动清除。

问题是当您应该使用Utils.CheckSession方法重定向时转发到admin.jsp页面

request.getRequestDispatcher("Admin.jsp").forward(request, response);

response.sendRedirect("Admin.jsp");
return false;

// in the doGet method
if (!Utils.CheckSession(request,response)) {
    return;
}

重定向不会立即发生,servlet将继续执行,当它到达下一个RequestDispatcher.forward来电时,会引发exception

服务器需要在http响应中发送http:redirect状态代码,然后浏览器接收响应并请求重定向URL指定的资源。

答案 1 :(得分:0)

在您的网址上加入一个斜线,即'/'。我遇到了同样的问题,我的救援工作就出现了一个简单的斜线。