Root URl of the servlet

后端 未结 5 752
面向向阳花
面向向阳花 2020-12-03 02:49

I want to get the root url of my web application from one of the servlet.

If I deploy my application in \"www.mydomain.com\" I want to get the root url like \"http:/

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 03:22

    This function helps you to get your base URL from a HttpServletRequest:

      public static String getBaseUrl(HttpServletRequest request) {
        String scheme = request.getScheme() + "://";
        String serverName = request.getServerName();
        String serverPort = (request.getServerPort() == 80) ? "" : ":" + request.getServerPort();
        String contextPath = request.getContextPath();
        return scheme + serverName + serverPort + contextPath;
      }
    

提交回复
热议问题