Root URl of the servlet

后端 未结 5 754
面向向阳花
面向向阳花 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:13

    public static String getBaseUrl(HttpServletRequest request) {
        String scheme = request.getScheme();
        String host = request.getServerName();
        int port = request.getServerPort();
        String contextPath = request.getContextPath();
    
        String baseUrl = scheme + "://" + host + ((("http".equals(scheme) && port == 80) || ("https".equals(scheme) && port == 443)) ? "" : ":" + port) + contextPath;
        return baseUrl;
    }
    

提交回复
热议问题