Solved

Servlets and RequestDispatcher.include()

Posted on 2001-09-13
2
771 Views
Last Modified: 2013-11-24
Hi,

I am having a problem with the RequestDispatcher and the include() method.

Whenever I execute the include() method, the response gets comitted immediatly after finishing processing of the include. I have tried increasing the buffer size dramatically.  This prevents me from using the forward() method

I have just installed JRun 3.1 and this code now throws the following exception....(although it worked perfectly in Jrun 3.0 Sp2a.)

java.lang.IllegalStateException: Response has already been committed
        at allaire.jrun.servlet.JRunResponse.resetContent(../servlet/JRunResponse.java:172)
        at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:1201)
        at allaire.jrun.servlet.JRunRequestDispatcher.forward(../servlet/JRunRequestDispatcher.java:89)
        at com.concept.mercedes.servlet.DispatcherServlet.service(DispatcherServlet.java:102)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1416)
        at allaire.jrun.session.JRunSessionService.service(../session/JRunSessionService.java:1082)
        at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:1270)
        at allaire.jrun.servlet.JRunNamedDispatcher.forward(../servlet/JRunNamedDispatcher.java:39)
        at allaire.jrun.servlet.Invoker.service(../servlet/Invoker.java:84)
        at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1416)
        at allaire.jrun.session.JRunSessionService.service(../session/JRunSessionService.java:1082)
        at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:1270)
        at allaire.jrun.servlet.JRunRequestDispatcher.forward(../servlet/JRunRequestDispatcher.java:89)
        at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1552)
        at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1542)
        at allaire.jrun.servlet.JvmContext.dispatch(../servlet/JvmContext.java:364)
        at allaire.jrun.jrpp.ProxyEndpoint.run(../jrpp/ProxyEndpoint.java:388)
        at allaire.jrun.ThreadPool.run(../ThreadPool.java:272)
        at allaire.jrun.WorkerThread.run(../WorkerThread.java:75)

Through debugging I have narrowed it down to the commented lines in the souce below (look for NOTE...)

  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.err.println("Executing DispatcherServlet...");

    response.setContentType("text/plain");

    String[] controllers = request.getParameterValues("controller");
    String view = request.getParameter("view");
    String originatingPageView = request.getParameter("originatingPageView");
    RequestDispatcher rd = null;

    try {
      // process all the controllers
      for (int i = 0; i < controllers.length; i ++) {

        rd = getServletContext().getRequestDispatcher(controllers[i]);

        if (rd == null) {
          System.err.println("DispatcherServlet error - No controller named : " + controllers[i]);
        } else {
          // invoke the controller
          System.err.println("Invoking : " + controllers[i]);
          // NOTE : response not commited here
          rd.include(request, response);
          // NOTE : response commited here
        }
      }

      // forward to the appropriate view appending any querystring info that might have been added to the request object
      QueryString qString = (QueryString)request.getAttribute("queryString");
      if (qString != null) {
        view = view + qString.toString();
      }

      rd = getServletContext().getRequestDispatcher(view);

      if (rd == null) {
        System.err.println("DispatcherServlet error - No view named : " + view);
      } else {
        System.err.println("Forwarding without errors : " + view);
        // NOTE : Fails here as already comitted.
        rd.forward(request, response);
      }

    } catch (ServletException e) {

      System.err.println("Root Cause --------------- : " + e.getRootCause());
      QueryString qString = (QueryString)request.getAttribute("queryString");
      // don't allow null values for qString object.
      if (qString == null) qString = new QueryString();

      if (e.getRootCause() instanceof InvalidParameterException) {

        // String redirect = originatingPageView;
        rd = getServletContext().getRequestDispatcher(originatingPageView + qString);

      } else if (e.getRootCause() instanceof SessionTimedOutException) {

        rd = getServletContext().getRequestDispatcher(loginView + qString);
      } else {

        rd = getServletContext().getRequestDispatcher(errorView + qString);
      }

      if (rd == null) {
        System.err.println("DispatcherServlet error - No errorView named : " + originatingPageView);
      } else {
        request.setAttribute("exception", e.getMessage());
        System.err.println("Forwarding with errors : " + errorView);
        rd.forward(request, response);
      }
    }
  }

If anyone has any insight, that would be great.
0
Comment
Question by:robinM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:robinM
ID: 6480883
I have just had a break through, deep in the depths of the allaire knowledge base is the following article which points to a hotfix.

                     http://www.allaire.com/Handlers/index.cfm?ID=21853&Method=Full

                     JRun Hot Fixes Build 16777

                     Fixes the following bug that was causing me errors...

                     26529 - 3.1 Regression. Servlet using getRequestDispatcher with an unbounded array of URLs causes "response has been closed" in Jrun 3.1. ex: RequestDispatcher dispatcher =
                     req.getRequestDispatcher(urlstrings);

                     After the install, all was good again....
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 6480906
Adding to PAQ Database
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question