code: public static String getServletURL (HttpServletRequest req, String path)
      {
          StringBuffer buf = req.getRequestURL();
          String sname = req.getServletPath();
          buf.delete(buf.length() - sname.length(), buf.length());
          if (!path.startsWith("/")) {
              buf.append("/");
          }
          buf.append(path);
          return buf.toString();
      }
docs: Prepends the server, port and servlet context path to the supplied
  path, resulting in a fully-formed URL for requesting a servlet.