Thursday, 11 November 2021

Java: How to get the domain name from url?

URL stands for Uniform resource locator, it is a special type of uri which tells how to access the resource.

 

Example

https://self-learning-java-tutorial.blogspot.com/search?q=test

 

In the above example.

https -> specific scheme

self-learning-java-tutorial.blogspot.com -> represents the path

/search -> specifies the path

q=test -> specifies query parameter.

 

Example 2

https://www.self-learning-java-tutorial.blogspot.com/search?q=test

 

In the above example, url contains the string www after //.

 

We need to address both the examples while finding the domain name. Below snippet takes an url and return the domain name.

public static String getHostName(String url) throws URISyntaxException {
	URI uri = new URI(url);
	String hostComponent = uri.getHost();
	return hostComponent.startsWith("www.") ? hostComponent.substring(4) : hostComponent;
}

 

Find the below working application.

 

DomainNameDemo.java

 

package com.sample.app.net;

import java.net.URI;
import java.net.URISyntaxException;

public class DomainNameDemo {

	public static String getHostName(String url) throws URISyntaxException {
		URI uri = new URI(url);
		String hostComponent = uri.getHost();
		return hostComponent.startsWith("www.") ? hostComponent.substring(4) : hostComponent;
	}

	public static void main(String args[]) throws URISyntaxException {
		String url1 = "https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";
		String url2 = "http://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";
		String url3 = "https://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";
		String url4 = "http://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html";

		System.out.println("Host name in " + url1 + " is \t" + getHostName(url1));
		System.out.println("Host name in " + url2 + " is \t\t" + getHostName(url2));
		System.out.println("Host name in " + url3 + " is \t" + getHostName(url3));
		System.out.println("Host name in " + url4 + " is \t" + getHostName(url4));
	}

}

 

Output

Host name in https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 	self-learning-java-tutorial.blogspot.com
Host name in http://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 		self-learning-java-tutorial.blogspot.com
Host name in https://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 	self-learning-java-tutorial.blogspot.com
Host name in http://www.self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html is 	self-learning-java-tutorial.blogspot.com

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment