Building an absolute URL from a relative URL in Java

后端 未结 4 1689
一向
一向 2020-12-03 14:35

I\'m having trouble building an absolute URL from a relative URL without resorting to String hackery...

Given

http://localhost:8080/myWebApp/someSer         


        
4条回答
  •  囚心锁ツ
    2020-12-03 14:46

    Looks like you already figured out the hard part, which is what host your are running on. The rest is easy,

    String url = host + request.getContextPath() + "/someImage.jpg";
    

    Should give you what you need.

提交回复
热议问题