Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL getHost() Method with Examples



Description

The Java URL getHost() method returns the host name of this URL, if applicable. The format of the host conforms to RFC 2732, i.e. for a literal IPv6 address, this method will return the IPv6 address enclosed in square brackets ('[' and ']').

Declaration

Following is the declaration for java.net.URL.getHost() method

public String getHost()

Parameters

NA

Return Value

the host name of this URL.

Exception

NA

Example 1

The following example shows the usage of Java URL getHost() method for a valid url with https protocol. In this example, we're creating an instances of URL class. Now using getHost() method, we're getting the host name and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         String hostName = url.getHost();
         System.out.println(hostName);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

www.tutorialspoint.com

Example 2

The following example shows the usage of Java URL getHost() method for a valid url with query parameters. In this example, we're creating an instances of URL class. Now using getHost() method, we're getting the host name and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         String hostName = url.getHost();
         System.out.println(hostName);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

www.tutorialspoint.com

Example 3

The following example shows the usage of Java URL getHost() method for a valid url with file protocol. In this example, we're creating an instances of URL class. Now using getHost() method, we're getting the host name and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("file","www.tutorialspoint.com","/index.htm");
         String hostName = url.getHost();
         System.out.println(hostName);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

www.tutorialspoint.com
java_url.htm
Advertisements