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
Solved

Servlets and RequestDispatcher.include()

Posted on 2001-09-13
2
767 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
oracle 11g 23 107
use lov values 2 61
login form jsp example 2 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

860 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