Home >Java >javaTutorial >Java.net URI

Java.net URI

PHPz
PHPzOriginal
2024-08-30 16:04:58387browse

Java.net package offers a class URI that contains methods for URI instance creation from its components or parsing string form of those components to access and retrieve several components of a URI instance.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is URI?

The uniform Resource Identifier has a sequence of characters that helps in a specific resource identification. Moreover, it helps in the interaction of the resource representation over the network with the help of certain protocols. More details on this particular class will be discussed in the below sections.

Syntax

Below is the syntax of the java.net URI class.

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
  • scheme: This component lays out the specific protocols that are linked with the URI. Even though “//” are required for some schemes, it is not used in others.
  • authority: This component is made up of different components such as authentication part, host, and a port number preceded by a colon ‘:’. In the first section, the Authentication section consists of a username and password. At the same time, the second section Host can be any of the ip address. The third section port number is an optional one.
  • path: The component which has a string that consists of the address present within the server to the particular resource.
  • query: This component is nonhierarchical data in which the query used for finding a specific resource by a ‘?’ question mark from the preceding part.
  • fragment: The component that identifies the secondary resources. It can be headings as well as subheadings present on a page etc.

How does the Java.net URI class work?

As already discussed, URI is known as the Uniform Resource Locator, which helps identify a web resource such as images, Cascade Style Sheet files, videos, etc., by using the locations and names. Here, the location is the mechanism to access or retrieve specific resources. For example,  HTTP://, FTP://, etc are the locations. In addition to that, “name” is the globally specific name of the particular resource. Let us see an example, where the following web address: https://samplepath/path/to/video.mkv retrieves a video. Here, sample-path/path/to/video.mkv section of this particular address is the URN or name.

Constructors

The following are the five different constructors of URI.

  • URI(String st): A URI will be constructed by parsing the string st.
  • URI( String scheme , String str, String frag): A URI will be constructed from the components given.
  • URI( String schm, String userinf, String host, int portnum, String path, String query, String frag): A hierarchical URI will be constructed from the components given.
  • URI( String schm, String host, String path, String frag): A hierarchical URI will be constructed from the components given.
  • URI( String schm, String suthorty, String path, String query, String frag): A hierarchical URI will be constructed from the components given.

Methods to Java.net URI

Below are the different methods of the URI class that performs several operations.

  • compareTo(URI ur): This URI will be compared with another URI.
  • create(String str): A URI will be created on parsing the string str
  • equals(Object obj): The URI will be tested for equality with the object obj
  • getAuthority(): Returns the URI’s authority component, which is decoded.
  • getFragment(): Returns the URI’s fragment component, which is decoded.
  • getHost(): Returns the URI’s host component, which is decoded.
  • getPath(): Returns the URI’s path component, which is decoded.
  • getPort(): URI port number will be returned.
  • getQuery(): Returns the URI’s query component, which is decoded.
  • getRawAuthority(): Returns the URI’s raw authority component.
  • getRawFragment(): Returns the URI’s raw fragment component.
  • getRawPath(): Returns the URI’s raw path component.
  • getRawQuery(): Returns the URI’s raw query component.
  • getRawSchemeSpecificPart(): Returns the URI’s raw scheme-specific part.
  • getRawUserInfo(): Returns the URI’s raw user information component.
  • getScheme(): Returns the URI’s scheme component.
  • getSchemeSpecificPart(): Returns the URI’s scheme-specific part which is decoded.
  • getUserInfo(): Returns the URI’s user information component which is decoded.
  • hashCode(): Returns URI hash code value.
  • isAbsolute(): Checks whether the URI provided is absolute or not.
  • isOpaque(): Checks whether the URI provided is opaque or not.
  • normalize(): URI path gets normalized on calling this method.
  • parseServerAuthority(): This method parses the authority component into user information, port, and host components.
  • relativize(URI uri): Given URI gets relativized to the URI specified.
  • resolve(String str): A new URI is constructed by parsing the string mentioned and resolving the same against the URI.
  • resolve(URI uri): The given URI is resolved against the URI.
  • toASCIIString(): Content of the URI mentioned is returned as a US-ASCII string.
  • toString(): Content of the URI mentioned is returned as a string.
  • toURL(): A URL gets constructed from the URI mentioned.

Examples to Implement Java.net URI

Now, let us see a sample program of java.net uri class.

Java program to demonstrate several methods in the class java.net.uri. //Java Program to demonstrate methods of URI class

Code:

import java.net.*;
class URIexample
{
public static void main(String[] args) throws Exception
{
String str = "https://www.google.com/search?q=educba+treemap&oq=educba+treemap+&aqs=chrome..69i57j69i60.12298j1j7&sourceid=chrome&ie=UTF-8";
// Creation of new URI by parsing the string
URI exampleuri = new URI(str);
//different methods of <u>uri</u> class
System.out.println("Host of the URI is:  " + exampleuri.getHost());
System.out.println("Port of the URI is: = " + exampleuri.getPath());
System.out.println("Raw Path of the URI is: = " + exampleuri.getRawPath());
System.out.println("Path of the URI is: = " + exampleuri.getPath());
System.out.println("Query of the URI is: = " + exampleuri.getQuery());
System.out.println("Raw Query of the URI is: = " + exampleuri.getRawQuery());
System.out.println("Fragment of the URI is: = " + exampleuri.getFragment());
System.out.println("Raw Fragment of the URI is: = " + exampleuri.getRawFragment());
//another uri in order to demonstrate the method compareTo and equals
URI exampleuri2 = new URI(str + "fr");
System.out.println("CompareTo exampleuri2=" + exampleuri.compareTo(exampleuri2));
System.out.println("Equals of the URI is: = " + exampleuri.equals(exampleuri2));
System.out.println("Hashcode of the URI is: " + exampleuri.hashCode());
System.out.println("toString of the URI is: " + exampleuri.toString());
System.out.println("toASCIIString of the URI is: " + exampleuri.toASCIIString());
}
}

Output: In this program, different methods of java.net.uri class are used and displayed results based on that.

Java.net URI

The above is the detailed content of Java.net URI. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Object Cloning in JavaNext article:Object Cloning in Java