  public static void selectApplication(HttpServletRequest request,
                                       ServletContext context) {

    // Acquire the path used to compute the module
    String matchPath = (String)
        request.getAttribute(RequestProcessor.INCLUDE_SERVLET_PATH);

    if (matchPath == null) {
      matchPath = request.getServletPath();
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug("Selecting module for path " + matchPath);
    }

    String prefix = "";  // Initialize prefix before we try lookup
    String prefixes[] = getApplicationPrefixes(context); // Get all other possible prefixes
    int lastSlash = 0;  // Initialize before loop

    while (prefix.equals("") && ((lastSlash = matchPath.lastIndexOf("/")) != 0)) {

      // We may be in a non-default sub-app.  Try to get it's prefix.
      matchPath = matchPath.substring(0, lastSlash);

      // Match against the list of module prefixes
      for (int i = 0; i < prefixes.length; i++) {
        if (matchPath.equals(prefixes[i])) {
          prefix = prefixes[i];
          break;
        }
      }
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug("Activating module " +
                (prefix.equals("") ? "default" : prefix));
    }

    // Expose the resources for this module
    selectApplication(prefix, request, context);

  }

