11

I have a URL like http://hostname:port_no/control/login.jsp.

I have the above url stored in some String.Now, I need to extract hostname from the String.

I am doing like this in my Java code

String domain = url.substring(url.indexOf('/') + 2, url.lastIndexOf(':'));

I want to know if there is any better way to do the same.

2
  • 1
    You could try using: URL class for parsing. Commented Apr 15, 2014 at 9:11
  • I request you to change subject of question to 'Get extract host name/domain name from url string' please do needfull Commented Apr 16, 2014 at 4:52

7 Answers 7

21

You can use the java.net.URI-class to extract the hostname from the string.

Here below is a method from which you can extract your hostname from a string.

public String getHostName(String url) {
    URI uri = new URI(url);
    String hostname = uri.getHost();
    // to provide faultproof result, check if not null then return only hostname, without www.
    if (hostname != null) {
        return hostname.startsWith("www.") ? hostname.substring(4) : hostname;
    }
    return hostname;
}

This above gives you the hostname, and is faultproof if your hostname does start with either hostname.com/... or www.hostname.com/..., which will return with 'hostname'.

If the given url is invalid (undefined hostname), it returns with null.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, Using URI is safer (and better) than using URL alone.
8
java.net.URL aURL;
try {
    aURL = new java.net.URL("http://example.com:80/docs/");
    System.out.println("host = " + aURL.getHost()); //example.com
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

1 Comment

you can use this or you can use regular expressions. Look for class Pattern and Matcher.
7
java.net.URL u = new URL("http://hostname:port_no/control/login.jsp");
System.err.println(u.getHost());

4 Comments

System.out.println()??
@MaciejCygan I believe it's an awesome troll.
@ambigram_maker indeed it is ;)
all good but why Sys.err ?? A: It is a valid use case for the method getHost(). It is an example.
2

A solution using Regular expression and group:

    String pattern = "(\\w*://)([\\w-_.]+)([:\\w\\W]*)";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(a);
    if (m.find())
    {
        System.out.println(m.group(0));
        System.out.println(m.group(1));
        System.out.println(m.group(2));
        System.out.println(m.group(3));
    }

group(2) is the hostname.

Comments

1

@KarelG's answer is the best answer, though I had specific issues with certain non-standard domains. The example issue is self contained below.

For certain "real world" input values, I had to add a check to the URI scheme to avoid mis-parsing of some addresses. This is the changed code.

import java.net.URI;
import java.net.*;

public class Domain {
    public static String getDomainName(String url) {
        try {
            URI uri = new URI(url);
            String domain = uri.getHost();
            System.out.println("domain: " + domain);
            if (uri.getScheme() != null) {
                return domain.startsWith("www.") ? domain.substring(4) : domain;
            } else {
                return uri.getSchemeSpecificPart();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

Below is the test case and value that was failing.

Domain.java javac Domain.java java Domain

import java.net.URI;
import java.net.*;
import java.io.*;

public class Domain {

    public static String longname = "https://www.3dprintingmedia.network/longform/page.html";
    public static String name = "www.3dprintingmedia.network";

    public static void getDomain(String url) {
        try {
                        URI uri = new URI(url);
                        String domain = uri.getHost();
            System.out.println("protocol: " + uri.getScheme());
            System.out.println("path: " + uri.getPath());
                        System.out.println("name: " + name);
                        System.out.println("domain: " + domain);
                        System.out.println(domain.startsWith("www.") ? domain.substring(4) : domain);
        } catch (Exception e) {
                        e.printStackTrace();
        }
   }

    public static void main(String[] args) {
       System.out.println("Parsing domain: " + name); 
       getDomain(longname);
       getDomain(name);
       System.exit(0);
    }
}

Comments

0

If you want string work, then try the following code sample,

String URL= "http://hostname:port_no/control/login.jsp";
String s_URL[] = ULR.split("//");
String s1 = s_URL[1];
String s2[] = s1.split(":");
String hostname = s2[0];

Comments

0

In Java:

String hostname = url.split("://")[1].split(":")[0];
String portnumber = url.split("://")[1].split(":")[1].split("/")[0];

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.