Thursday, October 20, 2011

Validate a URL in Java

To validate a URL String in Java we can use the java.net.URL object. First a URL object is created, passing the URL String to its constructor. If the URL String is malformed or invalid, a MalformedURLException will be thrown indicating a malformed URL. Next the URL is converted to a URI using the toURI() method. If URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI, a URISyntaxException will be thrown indicating a malformed URL. The following code demonstrates how to validate a URL String.


package url;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

//
// The following example code demonstrates how to
// check if a given URL is valid or not.
//
public class CheckURLValidity {

    public static void main(String[] args) {

        String testURLString = "https://thermodynamiccomputing.com/main?&t=20&f=52";
        System.out.println(isValidURL(testURLString));

        testURLString = "http6://thermodynamiccomputing.com/";
        System.out.println(isValidURL(testURLString));

        testURLString = "http://thermodynamiccomputing.com/   dd";
        System.out.println(isValidURL(testURLString));

        testURLString = "http://<thermodynamiccomputing.com";
        System.out.println(isValidURL(testURLString));
    }

    private static boolean isValidURL(String pUrl) {

        URL u = null;
        try {
            u = new URL(pUrl);
        } catch (MalformedURLException e) {
            return false;
        }
        try {
            u.toURI();
        } catch (URISyntaxException e) {
            return false;
        }
        return true;
    }

}

Here is the output of the example code:
true
false
false
false
Piece of cake!!!

Sunday, October 16, 2011

Get Domain Name from a URL String in Java

To get the domain name or sub-domain name from a URL String in Java we can use the java.net.URL object. First a URL object is created, passing the URL String to its constructor. If the URL String is malformed or invalid, a MalformedURLException will be thrown. The getHost() method is then used to pull only the part of the URL containing the domain name. The following code demonstrates getting the domain name from a URL String.


import java.net.MalformedURLException;
import java.net.URL;

//
// The following example code demonstrates how to
// get the domain name from a URL String
//
public class GetDomainNameFromURL {

    public static void main(String[] args) throws MalformedURLException {

        String testURLString = "https://thermodynamiccomputing.com/main?&t=20&f=52";
        URL lURL = new URL(testURLString);
        String hostName = lURL.getHost();
        System.out.println(hostName);

        testURLString = "http://www.thermodynamiccomputing.com/main?&t=20&f=52";
        lURL = new URL(testURLString);
        hostName = lURL.getHost();
        System.out.println(hostName);

        testURLString = "http://www.sub.thermodynamiccomputing.com/main?&t=20&f=52";
        lURL = new URL(testURLString);
        hostName = lURL.getHost();
        System.out.println(hostName);

    }
}



Here is the output of the example code:
thermodynamiccomputing.com
www.thermodynamiccomputing.com
www.sub.thermodynamiccomputing.com
Piece of cake!!!