How to get URL of site using java Programs

import java.io.*;
import java.net.*;

public class URLDemo {
  public static void main(String[] args) {
    try {
      URL url = new URL("http://www.techSarvam.com/java");
      System.out.println("Protocol: " + url.getProtocol());
      System.out.println("Host Name: " + url.getHost());
      System.out.println("Port Number: " + url.getPort());
      System.out.println("File Name: " + url.getFile());
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

1. Import Statements

import java.io.*;

import java.net.*;

These are the imports for the program. The java.net.* import brings in classes like URL that are required to handle URL operations. java.io.* is imported, although not used in this program explicitly.

2. Class Definition

public class URLDemo

This is a public class of the name URLDemo.

3. Main Method

public static void main(String[] args){

The main method is the entry point for the Java program. It is where the program starts the execution.

4. URL Object Creation

URL url = new URL(“http://www.TechSarvam.com/java”);

Here the given URL is used to create a URL object, “http://www.TechSarvam.com/java”. Here this URL includes

  • Protocol: http
  • Host: www.TechSarvam.com
  • File: /java
  • Port: It doesn’t specify the port number, so by default, it will be -1 as it doesn’t exist in the URL.

5. Statements for Output

System.out.println(“Protocol: ” + url.getProtocol());

System.out.println(“Host Name: ” + url.getHost());

System.out.println(“Port Number: ” + url.getPort());

System.out.println(“File Name: ” + url.getFile());

  • url.getProtocol() returns the protocol part of the URI, which is http.
  • url.getHost() returns the host name, which is www.TechSarvam.com
  • url.getPort() returns the port number. Since port number doesn’t exist in this URI, it will return -1.
  • url.getFile() returns the file part of the URL, that is /java.

6. Exception Handling

catch(Exception e){

   System.out.println(e);

}

The try-catch is used to handle potential exceptions. If any exception arises-for example, the format of the URL is incorrect-therefore, the exception will be caught and printed out.

7. Program Output

When the program is running, it prints the following information according to the URL:

Protocol: http

Host Name: www.TechSarvam.com

Port Number: -1

File Name: /java

Explanation of Output:

  • Protocol: http is the protocol used in the URL.
  • Host Name: www.TechSarvam.com is the domain name (host) part of the URL.
  • Port Number: Since the port number is not specified in the URL, it returns -1, which is the default for URLs without an explicit port.
  • File Name: /java is the path specified after the domain.

Conclusion:

This is a simple demonstration of how one can extract the URL components in Java, using the URL class. The class can be explained well by its use with protocol, hostname, port, and file path for any given URL.

Leave a Comment

Your email address will not be published. Required fields are marked *