code: private String setUpAFullUrl(HttpServletRequest req, String loginErrorPage, boolean bCtx) {
          String errorPage = null;
          if (loginErrorPage != null) {
              if (loginErrorPage.startsWith("http://") || loginErrorPage.startsWith("https://")) {
                  return loginErrorPage;
              }
              if (!loginErrorPage.startsWith("/"))
                  loginErrorPage = "/" + loginErrorPage;
              StringBuffer URL = req.getRequestURL();
              String URLString = URL.toString();
  
              int index = URLString.indexOf("//");
              index = URLString.indexOf("/", index + 2);
              int endindex = URLString.length();
  
              if (bCtx) {
                  String ctx = req.getContextPath();
                  if (ctx.equals("/"))
                      ctx = "";
                  errorPage = ctx + loginErrorPage;
              } else {
                  errorPage = loginErrorPage;
              }
  
              URL.replace(index, endindex, errorPage);
              errorPage = URL.toString();
          }
          return errorPage;
      }
docs: Set up an error page as a full URL (http;//host:port/ctx/path)
  
  @param req
  @param loginErrorPage
  @return errorPage