?
Solved

Servlets and RequestDispatcher.include()

Posted on 2001-09-13
2
Medium Priority
?
773 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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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