resolve(URI): This method is available in the java.net.URI class of Java.
Syntax:
URI java.net.URI.resolve(URI uri)
This method takes one argument. This method resolves the given URI against this URI
If the given URI is already absolute, or if this URI is opaque, then the given URI is returned.
1. A new URI is constructed with this URI's scheme and the given URI's query and fragment components.
2. If the given URI has an authority component then the new URI's authority and path are taken from the given URI.
3. Otherwise the new URI's authority component is copied from this URI and its path is computed as follows:
a). If the given URI's path is absolute then the new URI's path is taken from the given URI.
b). Otherwise the given URI's path is relative, and so the new URI's path is computed by resolving the path of the given URI against the path of this URI.
This is done by concatenating all but the last segment of this URI's path, if any, with the given URI'spath and then normalizing the result as if by invoking the normalize method.
The result of this method is absolute if, and only if, either this is absolute or the given URI is absolute.
Parameters: One parameter is required for this method.
uri: The URI is to be resolved against this URI.
Returns: The resulting URI.
Throws:
1. NullPointerException - If uri is null
Approach 1: When no exception
Java
package com.URI;import java.net.URI;import java.net.URISyntaxException;public class URIresolve2 {public static void main(String[] args) throws URISyntaxException {String scheme = "https", userInfo = "userInfo", host = "localhost", path = "/hello", query = "param=value",fragment = "fragment";int port = 8080;URI uri = new URI(scheme, userInfo, host, port, path, query, fragment);System.out.println(uri.resolve(uri));}}
Output:
https://userInfo@localhost:8080/hello?param=value#fragment
Approach 2: NullPointerException
Java
package com.URI;import java.net.URI;import java.net.URISyntaxException;public class URIresolve2 {public static void main(String[] args) throws URISyntaxException {String scheme = "https", userInfo = "userInfo", host = "localhost", path = "/hello", query = "param=value",fragment = "fragment";int port = 8080;URI uri = new URI(scheme, userInfo, host, port, path, query, fragment);URI uri2 = null;System.out.println(uri.resolve(uri2));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URI.isOpaque()" because "child" is null at java.base/java.net.URI.resolve(URI.java:2151) at java.base/java.net.URI.resolve(URI.java:1068) at com.URI.URIresolve2.main(URIresolve2.java:15)
Some other methods of URI class
URI(String): This method constructs a URI by parsing the given string.
URI(String, String, String): This method constructs a URI from the given components. A component may be left undefined by passing null.
URI(String, String, String, String): This method constructs a hierarchical URI from the given components.
URI(String, String, String, String, String): This method constructs a hierarchical URI from the given components. If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'). Otherwise, a component of the new URI may be left undefined by passing null for the corresponding parameter.
URI(String, String, String, int, String, String, String): This method constructs a hierarchical URI from the given components. If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'). Otherwise, a component of the new URI may be left undefined by passing null for the corresponding parameter or, in the case of the port parameter, bypassing -1.
compareTo(URI): This method compares this URI to another object, which must be a URI.
URI.create(String): This method creates a URI by parsing the given string.
equals(Object): This method tests this URI for equality with another object.
getAuthority(): This method returns the decoded authority component of this URI.
getFragment(): This method returns the decoded fragment component of this URI.
getHost(): This method returns the host component of this URI.
getPath(): This method returns the decoded path component of this URI.
getPort(): This method returns the port number of this URI.
getQuery(): This method returns the decoded query component of this URI.
getRawAuthority(): This method returns the raw authority component of this URI.
getRawFragment(): This method returns the raw fragment component of this URI.
getRawPath(): This method returns the raw path component of this URI.
getRawQuery(): This method returns the raw query component of this URI.
getRawSchemeSpecificPart(): This method returns the raw scheme-specific part of this URI. The scheme-specific part is never undefined, though it may be empty.
getRawUserInfo(): This method returns the raw user-information component of this URI.
getScheme(): This method returns the scheme component of this URI.
hashCode(): This method returns a hash code value for this URI.
isAbsolute(): This method tells whether or not this URI is absolute.
isOpaque(): This method tells whether or not this URI is opaque.
normalize(): This method normalizes this URI's path.
parseServerAuthority(): This method attempts to parse this URI's authority component if defined, into user information, host, and port components.
relativize(URI): This method relativizes the given URI against this URI.
resolve(String): This method constructs a new URI by parsing the given string and then resolving it against this URI.
resolve(URI): This method resolves the given URI against this URI
toASCIIString(): This method returns the content of this URI as a US-ASCII string.
toString(): This method returns the content of this URI as a string.
toURL(): This method constructs a URL from this URI.
No comments:
Post a Comment