Easy Tutorial
❮ Java Hashmap Containskey Net Connected ❯

Java Example - Parsing URL

Java Examples

The following example demonstrates how to parse a URL using methods like url.getProtocol(), url.getFile(), etc. from the net.URL class:

Main.java File

import java.net.URL;

public class Main {
   public static void main(String[] args) 
   throws Exception {
      URL url = new URL("http://www.tutorialpro.org/html/html-tutorial.html");
      System.out.println("URL is " + url.toString());
      System.out.println("Protocol is " + url.getProtocol());
      System.out.println("File name is " + url.getFile());
      System.out.println("Host is " + url.getHost());
      System.out.println("Path is " + url.getPath());
      System.out.println("Port number is " + url.getPort());
      System.out.println("Default port number is " 
      + url.getDefaultPort());
   }
}

The output of the above code is:

URL is http://www.tutorialpro.org/html/html-tutorial.html
Protocol is http
File name is /html/html-tutorial.html
Host is www.tutorialpro.org
Path is /html/html-tutorial.html
Port number is -1
Default port number is 80

Java Examples

❮ Java Hashmap Containskey Net Connected ❯