// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.net.MalformedURLException;
import java.net.URL;

public class UrlConvert {
    public static String hostReplace(String urlstr, String newhost) throws MalformedURLException {
        URL url = new URL(urlstr);
        url = new URL(url.getProtocol(), newhost, url.getPort(), url.getFile());
        return url.toString();
    }
    private static void test(String urlstr, String newhost) throws MalformedURLException {
        System.out.println(urlstr + " -> " + hostReplace(urlstr, newhost));
    }
    public static void main(String[] args) throws MalformedURLException {
        test("http://localhost/webapp/service", "x.x.x");
        test("http://localhost:8080/webapp/service", "x.x.x");
        test("http://foo.com/webapp/service", "x.x.x");
        test("https://192.168.1.1:8181/webapp/service", "x.x.x");
        test("http://www.xxx.dk/foobar.php?id=123", "x.x.x");
    }
}
