Java Program to Get Components of a URL


URL known as Uniform Resource Locator is a string used to specify the location of web resources like web pages, images, videos, files on the internet.URL helps to easily access them and helps to retrieve the resources from the web servers.URL is also known as Internet address or web address. In this section, we will be discussing how to get the different components of a URL in Java.

Components of a URL

Below are the components of a URL −

  • Protocol − Protocol specifies which method is used to access the web resource. This method is used to specify the rules and regulations used for the communication between client and server. Example: HTTPS, HTTPS, FTP, SMTP

  • Host − The host identifies the domain of the resource which is basically the IP address. Example: www.google.com

  • Port − Port specifies the server which handles the request. It takes default server if no port is specified. Example −80, 443

  • Path − The path specifies the location of the web resource on the server as a path if file or directory.

  • Query string − This is used to pass parameters as name-value pairs to the web resource. The name value pairs are separated by &.

  • Fragment − It is used to locate specific part in the web resource and is identified by #

Example of URL

https://www.example.com/path/to/file.html?key=value#fragment

  • Protocol: https

  • Host: www.example.com

  • Port: 80

  • Path: /path/to/file.html

  • Query string: key=value

  • Fragment: fragment

Now, we will discuss various approaches to get different Components of URL using Java Programming Language.

Approach 1: Using URL Class

In this approach, we will be using ‘URL’ class in ‘java.net’ package. It provides various in-built functions to work with url. Now, we will be implementing a java program to find all the components.

Algorithm

  • Create a URL object using URL class

    • In try block, print the components using the below methods

      • getProtocol()

      • getHost())

      • getPort()

      • getPath()

      • getQuery()

      • getRef()

  • Catch the exceptions using catch() and print the message using getMessage() method.

Methods Used

getProtocol() − This method is used to get the protocol component of the URL.It returns a string.

URL url = new URL("https://www.example.com");
String protocol = url.getProtocol(); // gives us the protocol used in URL

getHost() − This method is used to get the host component of the URL.It returns a string.

URL url = new URL("https://www.example.com");
String host = url.getHost();

getPort() − This method is used to get the port number of the URL. It returns an Integer value.

URL url = new URL("https://www.example.com:8080");
int port = url.getPort();

getPath() − This method is used to get the path of the URL.

URL url = new URL("https://www.example.com/path/to/resource");
String path = url.getPath();

getQuery() − This method is used to get the query string of the URL.

URL url = new URL("https://www.example.com/path/to/resource?key1=value1&key2=value2");
String query = url.getQuery()

getRef() − This method is used to get the fragment of the URL.

URL url = new URL("https://www.example.com/path/to/resource?key1=value1&key2=value2#section1");
String fragment = url.getRef();

Example

In this example we create a URL object and used different methods on the created URL object to get different components of URL.

import java.net.URL;

public class Main {
   public static void main(String[] args) {
      try {
         URL url = new URL("https://www.example.com/path/to/file.html?key=value#fragment");
         System.out.println("Protocol: " + url.getProtocol());
         System.out.println("Host: " + url.getHost());
         System.out.println("Port: " + url.getPort());
         System.out.println("Path: " + url.getPath());
         System.out.println("Query: " + url.getQuery());
         System.out.println("Fragment: " + url.getRef());
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

Output

Protocol: https
Host: www.example.com
Port: -1
Path: /path/to/file.html
Query: key=value
Fragment: fragment

Approach 2: Using URI Class

In this approach, we will be using ‘URI’ class in ‘java.net’ package. It provides various in-built functions to work with url. Now, we will be implementing a java program to find all the components.

Algorithm

  • Create a URI object using URI class

  • In try block, print the components using below methods

    • getScheme()

    • getHost())

    • getPort()

    • getPath()

    • getQuery()

    • getFragment()

  • Catch the exceptions using catch() and print the message using getMessage() method.

Methods Used

getScheme() − This method is similar to getProtocol(). It is used to get protocol of the URL.

URL url = new URL("https://www.example.com/path/to/resource");
String scheme = url.getScheme();

getFragment() − This method is similar to getRef(). It is used to get the fragment of the URL.

URI uri = new URI("https://www.example.com/path/to/resource?key1=value1&key2=value2#section1");
 String fragment = uri.getFragment();

substring() − This method is used to get a short string from a larger string by taking start and end indexes of the substring as parameters.

String str = "Hello, world!";
String substr1 = str.substring(0, 5); // extracts "Hello"
String substr2 = str.substring(7); // extracts "world!"

indexOf() − This method is used to find the index of a particular character in a string.

String str = "Hello, world!";
int index1 = str.indexOf('o'); // finds the first occurrence of 'o'

getMessage() − This method is used to get an error message of the exception.

errorObject.getMessage()

Example

In the example, we create a URI object and used different methods on the created URL object to get different components of URL.

import java.net.URI;

public class Main{
   public static void main(String[] args) {
      try {
         URI uri = new URI("https://www.example.com/path/to/file.html?key=value#fragment");
         System.out.println("Scheme: " + uri.getScheme());
         System.out.println("Host: " + uri.getHost());
         System.out.println("Port: " + uri.getPort());
         System.out.println("Path: " + uri.getPath());
         System.out.println("Query: " + uri.getQuery());
         System.out.println("Fragment: " + uri.getFragment());
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

Output

Scheme: https
Host: www.example.com
Port: -1
Path: /path/to/file.html
Query: key=value
Fragment: fragment

Thus, in this article we have discussed different approaches to get the components of a URL using Java Programming Language.

Updated on: 16-Aug-2023

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements