Java URL Class: Working with Web Resources in Java

Explore the Java URL class, a powerful tool for handling Uniform Resource Locators. Understand the components of a URL—protocol, server, port, and file path—and learn how to retrieve and interact with these elements in your Java applications.



Java URL Class

The Java URL class represents a URL (Uniform Resource Locator). It points to a resource on the World Wide Web. For example:

https://www.tutorialsarena.com/java-tutorial

URL in Java

A URL contains various pieces of information:

  • Protocol: For example, http is the protocol.
  • Server name or IP Address: For example, www.tutorialsarena.com is the server name.
  • Port Number: It is optional. If we write http//www.tutorialsarena.com:80/adminuser/, 80 is the port number. If not mentioned, it returns -1.
  • File Name or Directory Name: For example, index.jsp is the file name.

Constructors of Java URL class

The URL class provides several constructors:

  • URL(String spec): Creates an instance of a URL from the String representation.
  • URL(String protocol, String host, int port, String file): Creates an instance of a URL from the given protocol, host, port number, and file.
  • URL(String protocol, String host, int port, String file, URLStreamHandler handler): Creates an instance of a URL from the given protocol, host, port number, file, and handler.
  • URL(String protocol, String host, String file): Creates an instance of a URL from the given protocol name, host name, and file name.
  • URL(URL context, String spec): Creates an instance of a URL by parsing the given spec within a specified context.
  • URL(URL context, String spec, URLStreamHandler handler): Creates an instance of a URL by parsing the given spec with the specified handler within a given context.

Commonly used methods of Java URL class

The java.net.URL class provides many methods. Important methods include:

Method Description
public String getProtocol() Returns the protocol of the URL.
public String getHost() Returns the host name of the URL.
public String getPort() Returns the port number of the URL.
public String getFile() Returns the file name of the URL.
public String getAuthority() Returns the authority of the URL.
public String toString() Returns the string representation of the URL.
public String getQuery() Returns the query string of the URL.
public String getDefaultPort() Returns the default port of the URL.
public URLConnection openConnection() Returns the instance of URLConnection associated with this URL.
public boolean equals(Object obj) Compares the URL with the given object.
public Object getContent() Returns the content of the URL.
public String getRef() Returns the anchor or reference of the URL.
public URI toURI() Returns a URI of the URL.

Example of Java URL class

Here is an example demonstrating the use of the URL class:

File: URLDemo.java

Syntax

import java.net.*;

public class URLDemo {
public static void main(String[] args) {
try {
    URL url = new URL("http://www.tutorialsarena.com/java-tutorial");

    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);
}
}
}
Output

Protocol: http
Host Name: www.tutorialsarena.com
Port Number: -1
File Name: /java-tutorial

Let us see another example of the URL class in Java:

File: URLDemo.java

Syntax

import java.net.*;

public class URLDemo {
public static void main(String[] args) {
try {
    URL url = new URL("https://www.google.com/search?q=tutorialsarena&oq=tutorialsarena&sourceid=chrome&ie=UTF-8");

    System.out.println("Protocol: " + url.getProtocol());
    System.out.println("Host Name: " + url.getHost());
    System.out.println("Port Number: " + url.getPort());
    System.out.println("Default Port Number: " + url.getDefaultPort());
    System.out.println("Query String: " + url.getQuery());
    System.out.println("Path: " + url.getPath());
    System.out.println("File: " + url.getFile());

} catch (Exception e) {
    System.out.println(e);
}
}
}
Output

Protocol: https
Host Name: www.google.com
Port Number: -1
Default Port Number: 443
Query String: q=tutorialsarena&oq=tutorialsarena&sourceid=chrome&ie=UTF-8
Path: /search
File: /search?q=tutorialsarena&oq=tutorialsarena&sourceid=chrome&ie=UTF-8