URL known as Uniform Resource Locator is simply a string of text that identifies all the resources on the Internet, telling us the address of the resource, how to communicate with it, and retrieve something from it.

Components of a URL
A URL can have many forms. The most general however follows a three-components system as proposed below:
- Protocol: HTTP is the protocol here
- Hostname: Name of the machine on which the resource lives.
- File Name: The pathname to the file on the machine.
- Port Number: Port number to which to connect (typically optional).
URL Class
The URL class is the gateway to any of the resources available on the internet. A Class URL represents a Uniform Resource Locator, which is a pointer to a “resource” on the World Wide Web. A resource can point to a simple file or directory, or it can refer to a more complicated object, such as a query to a database or to a search engine.
Constructors of the URL class
- URL(String address) throws MalformedURLException: It creates a URL object from the specified String.
- URL(String protocol, String host, String file): Creates a URL object from the specified protocol, host, and file name.
- URL(String protocol, String host, int port, String file): Creates a URL object from protocol, host, port, and file name.
- URL(URL context, String spec): Creates a URL object by parsing the given spec in the given context.
- URL(String protocol, String host, int port, String file, URLStreamHandler handler):
Creates a URL object from the specified protocol, host, port number, file, and handler.
- URL(URL context, String spec, URLStreamHandler handler):
Creates a URL by parsing the given spec with the specified handler within a specified context.
Important Methods used in URL class
| Method |
Action Performed |
| getAuthority() |
Returns the authority part of URL or null if empty |
| getDefaultPort() |
Returns the default port used |
| getFile() |
Returns the file name. |
| getHost() |
Return the hostname of the URL in IPv6 format |
| getPath() |
Returns the path of the URL, or null if empty |
| getPort() |
Returns the port associated with the protocol specified by the URL |
| getProtocol() |
Returns the protocol used by the URL |
| getQuery() |
the Returns the query part of URL. A query is a part after the ‘?’ in the URL. Whenever logic is used to display the result, there would be a query field in the URL. It is similar to querying a database. |
| getRef() |
Returns the reference of the URL object. Usually, the reference is the part marked by a ‘#’ in the URL. You can see the working example by querying anything on Google and seeing the part after ‘#’ |
| toString() |
As in any class, toString() returns the string representation of the given URL object. |
Example:
Java
import java.net.MalformedURLException;
import java.net.URL;
public class GFG {
public static void main(String[] args)
throws MalformedURLException
{
URL url1 = new URL(
+ "WK26I4fT8gfth6CACg#q=geeks+for+geeks+java");
URL url2 = new URL("http", "www.geeksforgeeks.org",
"/jvm-works-jvm-architecture/");
URL url3 = new URL(
+ "q=gnu&rlz;=1C1CHZL_enIN71"
+ "4IN715&oq;=gnu&aqs;=chrome..69i57j6"
+ "9i60l5.653j0j7&sourceid;=chrome&ie;=UTF"
+ "-8#q=geeks+for+geeks+java");
System.out.println(url1.toString());
System.out.println(url2.toString());
System.out.println();
System.out.println(
"Different components of the URL3-");
System.out.println("Protocol:- "
+ url3.getProtocol());
System.out.println("Hostname:- " + url3.getHost());
System.out.println("Default port:- "
+ url3.getDefaultPort());
System.out.println("Query:- " + url3.getQuery());
System.out.println("Path:- " + url3.getPath());
System.out.println("File:- " + url3.getFile());
System.out.println("Reference:- " + url3.getRef());
}
}
|
Output:
https://www.google.co.in/?gfe_rd=cr&ei=ptYqWK26I4fT8gfth6CACg#q=geeks+for+geeks+java
https://www.geeksforgeeks.org/jvm-works-jvm-architecture/
Different components of the URL3-
Protocol:- https
Hostname:- www.google.co.in
Default port:- 443
Query:- q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8
Path:- /search
File:- /search?q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&sourceid=chrome&ie=UTF-8
Reference:- q=geeks+for+geeks+java
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
18 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...