Get confluence server URL(Without context)

Is there any way to get the pure URL of the confluence server.

I want:
Instead of

Sometimes there is no defined context in URL. So we have something like this:

So I could not remove context programmatically.

Can you expand on what you’re trying to solve? Getting the localhost url might not be what you’re trying to do (you could always feed things into a URL object in java and get the data out of that).

I wanted do something on the resources(like images) of the confluence page. But some of their path begins with context. So if I use baseUrl in beginning of the these paths then I will get smething like this: http://myHost/myContext/myContext/rest or any thing/ … . So I need to get the only host name or know the context name to remove it from the path.
My question was about it that is there any way by atlassian to do it?

I created a static method for it. I put it here for whom maybe needs it:

    public static String getServerUrl(){
        try {
            URL url = new URL(GeneralUtil.getGlobalSettings().getBaseUrl());
            return url.getProtocol() + "://" + url.getHost() +
                    (url.getPort() == 80 ? "" : ":" + url.getPort());
        } catch (MalformedURLException e) {
            return GeneralUtil.getGlobalSettings().getBaseUrl();