code: private String getFedoraBaseURL(String requestURL) throws ServletException {
          int i = requestURL.lastIndexOf(_SERVLET_PATH);
          if (i != -1) {
              return requestURL.substring(0, i);
          } else {
              throw new ServletException("Unable to determine Fedora baseURL "
                      + "from request URL.  Request URL does not contain the "
                      + "string '" + _SERVLET_PATH + "', as expected.");
          }
      }
docs: Determine the base URL of the Fedora webapp given the request URL to this
  servlet.