Get the Host name, Port no, File name, Protocol of any URL in Advance java.

This Java tutorial will focus on the following topics:

  • How to find the hostname from URL
  • Get the port no from URL
  • Get File Name from URL
  • Find Protocol from URL

Get hostname, port no, file name, protocol from URL in Java

We will learn some concepts of advanced java. In this program, we will use the Net package. Url class is also used in this program. Now we will learn some methods which are used in this program. Now we start

  1. url.getProtocol()
  2. url.getPort()
  3. url.getFile()
  4. url.getHost()

Url.getProtocol gives the protocol of the given URL.
Url.getPort gives the port number of the given URL.
Url.getFile gives the File name of the given URL.
Url.getFile gives the Host address of the given URL.

URL class in java

The URL class is the gateway to any of the resource available on the internet. A class URL represents a uniform resource locator, which is a pointer to “resource” on the world wide web. A resource is able to point to a simple file or directory, or it can also refer to a more complicated object, like a query to a database or to a search engine.

Java program to get information from URL

 

import java.net.*;/*this is mandatory is we want to perform any operation related net*/


class Test
{
  public static void main(String s[])throws Exception
  {
    URL url =new URL ("https://www.youtube.com/watch?v=qoXavIc1M4o");/*This is the url it can be diffrent*/		
    System.out.println("protocol:"+url.getProtocol());/*This line gives the protocol of any Url*/
      System.out.println("Host name:"+url.getHost());/* This methos gives the host address of the url*/
        System.out.println("port:"+url.getPort());/*This methos gives the port number of the url*/
          System.out.println("filename"+url.getFile());/*
          This methos gives the File name of the url*/
  }
}

An output of this program

Protocol: https
Hostname: www.youtube.com
port:-1
Filename:/watch?v=qoXavIc1M4o

here we have used the URL as an example.




Also learn,


Leave a Reply

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