4 Replies Latest reply on Jul 10, 2005 12:16 PM by tmadi

    CoyoteResponseFacade.SendRedirect Yields IllegalStateExcepti

    elwallacejr

      I've recently installed JBoss 4.01 on WinXP System and have successfully gotton responses from test servlets using the PrintWriter. The problem I have is that my test servlet is derived from a superclass which extends HttpServlet which performs (or soon to perform) validation and state awareness. If during this process in the superclass an error occurs, I would perform a response.sendRedirect("/unknown.html"). Based on the servlet exception I receive, I know that the sendRedirect causes the exception but cannot find anything on the web which helps me out. I've attached the exception and code below.

      Thanks,
      Ernie

      Exception:

      2005-02-02 18:41:56,025 ERROR [org.jboss.web.localhost.Engine] StandardWrapperValve[MyServlet]: Servlet.service() for servlet MyServlet threw exception
      java.lang.IllegalStateException
      at org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect(CoyoteResponseFacade.java:352)
      at blueheronsoftware.EventCalendar.GBHSServlet.doPost(GBHSServlet.java:40)
      at blueheronsoftware.EventCalendar.MyServlet.doPost(MyServlet.java:29)
      at blueheronsoftware.EventCalendar.MyServlet.doGet(MyServlet.java:21)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:54)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)


      Derived Class Code:

      public class MyServlet extends GBHSServlet
      {
      public void doGet(
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException
      {
      doPost(request, response);
      }

      public void doPost(
      HttpServletRequest request,
      HttpServletResponse response)
      throws ServletException, IOException
      {
      Application app = null;
      super.doPost(request,response,app);
      PrintWriter out = response.getWriter();


      Super Class Code:

      public class GBHSServlet extends HttpServlet {
      protected void doPost(HttpServletRequest request, HttpServletResponse response, Application app) throws ServletException, IOException
      {
      try
      {
      app = new Application(this.toString(),"");
      if(!scrubRequest(request))
      response.sendRedirect("/unknown.html");
      }
      catch(ApplicationException ae)
      {
      response.sendRedirect("unknown.html");
      }
      catch(Exception e)
      {
      response.sendRedirect("unknown.html");

      }
      if(!scrubRequest(request))
      {

      response.sendRedirect("unknown.html");
      }
      }

      private boolean scrubRequest(HttpServletRequest request) throws ServletException
      {
      return false;
      }
      }

        • 1. Re: CoyoteResponseFacade.SendRedirect Yields IllegalStateExc
          starksm64

          org.apache.coyote.tomcat5.CoyoteResponseFacade.sendRedirect:

           public void sendRedirect(String location)
           throws IOException {
          
           if (isCommitted())
           throw new IllegalStateException();
          



          servlet-2.4 spec.

          public void sendRedirect(java.lang.String location)
          throws IOException
          Sends a temporary redirect response to the client using the specified redirect
          location URL. This method can accept relative URLs; the servlet container
          must convert the relative URL to an absolute URL before sending the
          response to the client. If the location is relative without a leading ?/? the container
          interprets it as relative to the current request URI. If the location is relative
          with a leading ?/? the container interprets it as relative to the servlet
          container root.

          If the response has already been committed, this method throws an Illegal-
          StateException. After using this method, the response should be considered
          to be committed and should not be written to.
          Parameters:
          location - the redirect location URL
          Throws:
          IOException - If an input or output exception occurs
          IllegalStateException - If the response was committed or if a partial URL
          is given and cannot be converted into a valid URL


          The isCommitted method returns a boolean value indicating whether any
          response bytes have been returned to the client. The flushBuffer method forces content in the buffer to be written to the client.

          • 2. Re: CoyoteResponseFacade.SendRedirect Yields IllegalStateExc
            elwallacejr

            Hi Scott,

            Based on the info you've sent, I've ensured that I haven't written anything to the buffer or even retrieved the printWriter. I've placed a return after the sendRedirect to ensure I never get to the getWriter method. I tried performing a flushBuffer to see if anything was in the buffer but received a blank page. Finally I used a absolute URL to ensure the resource was available and it was. What am I missing?

            Thanks for your help,
            Ernie

            • 3. Re: CoyoteResponseFacade.SendRedirect Yields IllegalStateExc
              starksm64

              Your missing the condition is that triggering the commit state of the request. Get out your debugger and find it or submit a bug report with a testcase that illustrates a valid sencario which is producing an IllegalStateException.

              • 4. Re: CoyoteResponseFacade.SendRedirect Yields IllegalStateExc
                tmadi

                Hi Scott

                Im using Jboss 4.0.2 running on WinXP and i do match the case here published by elwallacejr. as soon as i try to use response.sendRedirect(pageUrl)

                i receive the same exception:
                2005-07-11 01:40:04,465 [http-0.0.0.0-8080-3:org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/voting].[frontend]] ERROR - Servlet.service() for servlet frontend threw exception
                java.lang.IllegalStateException
                at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:423)
                at com.abomadi.voting.webapp.FrontEnd.service(FrontEnd.java:61)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
                at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
                at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
                at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                at java.lang.Thread.run(Thread.java:534)

                bear in mind that i did not event request the :'response.getWriter(); '