Java program to demonstrate UrlClass

This program demonstrate URL class in Java. A URL class is used to fetch information about a given URL. This program demonstrates all the functions of a URL class. The program declares an object, url of the of the URL class. Then the program applies various functions of the URL class to this object. The URL class id defined in java.net package. Below is a list of functions in the URL class an its description.

  • toString: This function is used to retrieve the whole url.
  • getProtocol: Every url starts with a protocol. This function retrieves the protocol from the url. Here the protocol is http.
  • getAuthority: Retrieves the authority of the url. 
  • getFile: Retrieves the file name of the url.
  • getHost: Retrieves the host of the url.
  • getPath: Retrieves the path of the url.
  • getPort: Retrieves the port number of the url.
  • getDefaultPort: Retrieves the default port of the url.
  • getQuery: Retrieves the query part in the url.
  • getRef: Retrieves the reference part of the url.


Program



//Url Class Java Program

import java.net.*;

class UrlClass

{

public static void main(String s[]) throws MalformedURLException

{

URL url=new URL("http://www.kerala.gov.in:85/index.html");

System.out.println("URL is:"+url.toString());

System.out.println("Protocol is:"+url.getProtocol());

System.out.println("Authority is:"+url.getAuthority());

System.out.println("File is:"+url.getFile());

System.out.println("Host is:"+url.getHost());

System.out.println("Path is:"+url.getPath());

System.out.println("Port is:"+url.getPort());

System.out.println("Default port is:"+url.getDefaultPort());

System.out.println("Query is:"+url.getQuery());

System.out.println("Ref is:"+url.getRef());

}

}


Output


Java program for URL class
URL class program in Java