Skip to content
Related Articles

Related Articles

Java Program to Get Components of a URL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 26 Nov, 2020
View Discussion
Improve Article
Save Article

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 ;

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() ;



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:


Protocol Used: http

Host name:

Path is file accessing : javaexaamples/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 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 Program to Get Components of a URL
import java.util.*;
// Importing URL class
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());


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!