How to get domain URL and application name?

后端 未结 6 964
挽巷
挽巷 2020-12-02 04:55

Here\'s the scenario.

My Java web application has following path

https://www.mywebsite.com:9443/MyWebApp

Let\'s say there is a JSP

6条回答
  •  悲&欢浪女
    2020-12-02 05:39

    Take a look at the documentation for HttpServletRequest.
    In order to build the URL in your example you will need to use:

    • getScheme()
    • getServerName()
    • getServerPort()
    • getContextPath()

    Here is a method that will return your example:

    public static String getURLWithContextPath(HttpServletRequest request) {
       return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
    }
    

提交回复
热议问题