Open In App

Java Program to Get Components of a URL

Improve
Improve
Like Article
Like
Save
Share
Report

Java Networking: As java programs are written to be executed across multiple devices whereby multiple devices are meant computers present in remote locations. So both devices can communicate be present in the same locations or different locations connected with some network. For java networking ‘net’ package needs to be used. It works on two different protocols namely TCP and UDP. TCP is preferred in java as it is a connection-oriented protocol while UDP is a connectionless protocol. Hence, reliability is less over UDP.

  • TCP stands for Transmission Control Protocol
  • UDP stands for User Datagram Protocol

Concept: URL Class and inbuilt getProtocol() method

URL Class is a uniform resource locator and it points to a resource on the World Wide Web.

Syntax: for importing URL class:

import java.util.net ;

getProtocol() The getProtocol() function is a part of URL class. The function getProtocol() returns the Protocol of a specified URL.

Function Signature:

public String getProtocol() ;

Syntax:

url.getProtocol();

Parameter: This function does not require any parameter

Return Type: The function returns String Type

Illustration: Now, further understanding through illustration the internal working of the URL class:

Considering Random URL: https://www.geeksforgeeks.com/javaexamples/net_singleuser.htm

Here, 

Protocol Used: http

Host name: www.geeksforgeeks.com

Path is file accessing : javaexamples/net_singleuser.htm

Port number: 443

Here, HTTP protocol is used as it can easily be seen from the name of the URL itself.

The java.net.URL class represents a URL. There are constructors to make new URLs and methods to parse the various parts of a URL. However, the guts of the category are the methods that allow you to urge an InputStream from a URL so you’ll read data from a server.

The URL class is closely tied to protocol and content handlers. The objective is to separate the info being downloaded from the protocol wont to download it. The protocol handler is liable for communicating with the server, that’s moving bytes from the server to the client. It handles any necessary negotiation with the server and any headers. Its job is to return only the actual bytes of the data or file requested. The content handler takes those bytes and translates them into some quite Java objects like an InputStream or ImageProducer.

Components of a URL: A URL can have many forms. The most general however follows a three-components system-

  1. Protocol: HTTP is the protocol here
  2. Hostname: Name of the system of the host.
  3. File Name: The pathname to the file on the system.
  4. Port Number: Port number to which to attach (typically optional).

Implementation: Below is the java code to get the parts of a URL:

Java




// Java Program to Get Components of a URL
 
import java.util.*;
// Importing URL class
import java.net.URL;
 
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
    {
        // Making object of URL type
        // URL url = new URL(args[0]);
 
        // Url taken for consideration as input URL
        URL url = new URL(
 
        // Print the string representation of the URL.
        System.out.println("URL is:" + url.toString());
 
        // Retrieve the protocol of URL
        System.out.println("protocol is: "
                           + url.getProtocol());
 
        // Retrieve the filename of URL
        System.out.println("file name is: "
                           + url.getFile());
 
        // Retrieve the hostname of URL
        System.out.println("host is: " + url.getHost());
 
        // Retrieve the path of URL
        System.out.println("path is: " + url.getPath());
 
        // Retrieve the port of URL
        System.out.println("port is: " + url.getPort());
        System.out.println("default port is: "
                           + url.getDefaultPort());
    }
}


Output

URL is:https://www.geeksforgeeks.com/javaexamples/net_singleuser.htm
protocol is: https
file name is: /javaexamples/net_singleuser.htm
host is: www.geeksforgeeks.com
path is: /javaexamples/net_singleuser.htm
port is: -1
default port is: 443


Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads