tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Wright <AtheneSyst...@btinternet.com>
Subject request dispatch woes
Date Sun, 03 Dec 2000 23:16:40 GMT
Hi

I have an application working well under Tomcat 3.1 and am trying to get it
to work under 3.2.

I had a sort of MVC thing going with a main servlet handling all requests
by handing off to specific controller servlets using
requestDispatcher.Include(request, response)  then returning to the user
the results of a JSP using requestDispatcher.Forward(request, response).

This approach worked just fine under 3.1 but fails under 3.2.

The the first step is OK but when the application tries to Forward it fails
with an error "Cannot forward as output stream or writer has already been
obtained".  If I comment out the requestDispatcher.Include statement the
JSP page returns OK but obvoiusly without any of the required changes to
the data having taken place.

Should I be able to do a requestDispatcher.Include followed by a
requestDispatcher.Forward?

Is anyone knowledgable enough to suggest whether this might be a bug?

If it is a bug how should I report it?

The code below indicates what I am doing - I apologise if any Java experts
find it offensive!

Alan Wright


  public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    // N.B. Declare variables in procedure not as member variables of
class.
    // This is to avoid thread issues in a multiuser environment (I hope!).

    String controller = "";  // servlet used to process request
    String viewName = "";    // presentation element for return to user
    String errorViewName = "error.jsp";  // presentation element for return
to user on error
    boolean authorisedMemberExpected = true;  // can we reasonably expect
authenticated memberInfo object to exist?
    RequestDispatcher rd;  //used for include of servlet processing or
forward of request to presentation element

    // Lets look at the request and set up environment prior to handling
    String requestPath = request.getPathInfo();

    System.out.println("in do get");

    if (requestPath.endsWith("Logout")) {
      controller = "/control/abernathy.Logout";
      viewName = "/loggedout.jsp";
      errorViewName = "/error.jsp";
      authorisedMemberExpected = false;
    }
    else if (requestPath.endsWith("TryCookieLogin")) {
     System.out.println("in TryCookieLogin");
      controller = "/control/abernathy.TryCookieLogin";
      viewName = "/memberinfo.jsp";
      errorViewName = "/loginerror.jsp";
      authorisedMemberExpected = false;
      //clear up any old session which could arise with multi users on one
PC
      HttpSession oldSession = request.getSession(false);
      if (oldSession != null) oldSession.invalidate();
     System.out.println("end TryCookieLogin");
    }
    else {
      controller = "";
      viewName = "/error.jsp";
      errorViewName = "/error.jsp";
    }

    // Create session if it doesn't exist
    HttpSession session = request.getSession();

    // if user should be authenticated make sure they are...
    if (authorisedMemberExpected) { //default is true except for login and
authentication
      MemberBean memberInfo = (MemberBean)
session.getAttribute("memberInfo");
      if ((memberInfo.equals(null)) || (!memberInfo.isAuthenticated())) {
        controller = "";
        viewName = "/authenticationrequired.jsp";
        errorViewName = "/error.jsp";
      }
    }


    // pass request to appropriate servlet for processing
    // then forward request to presentation element...
    try {
      // first we invoke the controller to perform any required
processing...
      if (!controller.equals("")) {
        rd = getServletContext().getRequestDispatcher(controller);
        if (rd == null) {
          getServletContext().log("No controller named " + controller);
          response.sendError(response.SC_NO_CONTENT);
        }
        else {
     System.out.println("in controller no rd");
          rd.include(request, response);
        }
      }
      // Before forwarding to the view we check to see if the servlet has
set
      // the request attribute for viewName in a catch statement. If this
is the
      // case we want to override the default viewName for the URL...
      String viewNameOverride = (String)
request.getAttribute("errorViewName");
      if (viewNameOverride != null) {
        viewName = viewNameOverride;
      }
      // next we forward responsability for generating response to the
client...
      rd = getServletContext().getRequestDispatcher(viewName);
      if (rd == null) {
        getServletContext().log("No view named " + viewName);
        response.sendError(response.SC_NO_CONTENT);
      }
      else {
     System.out.println("in view");
     System.out.println(request.getContextPath());
     System.out.println(request.getPathInfo());
     System.out.println(request.getPathTranslated());
     System.out.println(request.getRequestURI());
     System.out.println(request.getServerName());
     System.out.println(request.getServletPath());
     System.out.println(rd.toString());
        rd.forward(request, response);
      }
    }
    // if there is an error we forward to the named error view
    catch (Exception e) {
      request.setAttribute("exception", e.toString());
      rd = getServletContext().getRequestDispatcher(errorViewName);
      if (rd == null) {
        getServletContext().log("No error view named " + errorViewName);
        response.sendError(response.SC_NO_CONTENT);
      }
      else {
     System.out.println("in error");
     System.out.println(e.getMessage());
     //e.printStackTrace();
        getServletContext().log("Error response via" + errorViewName);
        //rd.forward(request, response);
      }
    }
  }



Mime
View raw message