![]() |
VOOZH | about |
The Java URL class, available in the java.net package, is used to represent and work with Uniform Resource Locators (URLs). It helps Java programs identify internet resources and access their components such as protocol, host, and path.
A URL can have many forms. The most general however follows a three-component system as proposed below:
The URL class is a core Java networking class used to access internet-based resources.
1. URL(String address): It creates a URL object from the specified String.
URL url = new URL("https://www.example.com/");
2. URL(String protocol, String host, String file): Creates a URL object from the specified protocol, host, and file name.
URL url = new URL("http", "www.example.com", "/path/to/resource");
3. URL(String protocol, String host, int port, String file): Creates a URL object from protocol, host, port, and file name.
URL url = new URL("https", "www.example.com", 443, "/path/to/resource");
4. URL(URL context, String spec): Creates a URL object by parsing the given spec in the given context.
URL baseUrl = new URL("https://www.example.com/");
URL relativeUrl = new URL(baseUrl, "/path/to/resource");
5. 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 = new URL("http", "www.example.com", 80, "/path/to/resource", new MyCustomHandler());
6. URL(URL context, String spec, URLStreamHandler handler): Creates a URL by parsing the given spec with the specified handler within a specified context.
URL baseUrl = new URL("https://www.example.com/");
URL relativeUrl = new URL(baseUrl, "/path/to/resource", new MyCustomHandler());
| Method | Description |
|---|---|
| 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() | Return the query part of the URL, which is the portion following the ? character, used to pass parameters to a web application. |
| getRef() | Return the reference part of the URL, which is the portion following the # character, typically used to navigate to a specific section within a web page. |
| toString() | As in any class, toString() returns the string representation of the given URL object. |
Example: Program to demonstrate the working of URL.
Output: