Solved

Servlets and RequestDispatcher.include()

Posted on 2001-09-13
2
762 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now