URL URL(URL, String) in Java

URL(URL, String): This method is available in the java.net.URL class of Java.

Syntax:

java.net.URL.URL(URL context, String spec) throws MalformedURLException

This method takes two arguments. This method creates a URL by parsing the given spec within a specified context.

The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers: Generic * Syntax" :

<scheme>://<authority><path>?<query>#<fragment>

The reference is parsed into the scheme, authority, path, query, and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise, the fragment and query parts present in the spec are used in the new URL. If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise, the scheme component is inherited from the context URL.

If the authority component is present in the spec then the spec is treated as absolute and spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context.

If the spec's path component begins with a slash character"/" then the path is treated as absolute and the spec path replaces the context path.

Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396. Also, in this case, the path is canonicalized through the removal of directory changes made by occurrences of ".." and ".".

Parameters: Two parameters are required for this method.

context: the context in which to parse the specification.

spec: the String to parse as a URL.

Throws:

1. MalformedURLException - if no protocol is specified, or an unknown protocol is found, or spec is null, or the parsed URL fails to comply with the specific syntax of the associated protocol.

Approach 1: When no exception

Java

package com.URL;

import java.net.MalformedURLException;
import java.net.URL;

public class URL2 {
    public static void main(String[] args) throws MalformedURLException {

        String spec = "http://localhost:8080/hello";
        URL context = new URL(spec);
        URL url = new URL(context, spec);

        System.out.println(url);
    }
}

Output:

http://localhost:8080/hello


Approach 2: MalformedURLException 

Java

package com.URL;

import java.net.MalformedURLException;
import java.net.URL;

public class URL2 {
    public static void main(String[] args) throws MalformedURLException {

        String spec = "://localhost:8080/hello";
        URL context = new URL(spec);
        URL url = new URL(context, spec);

        System.out.println(url);
    }
}

Output:

Exception in thread "main" java.net.MalformedURLException: no protocol: ://localhost:8080/hello at java.base/java.net.URL.<init>(URL.java:672) at java.base/java.net.URL.<init>(URL.java:568) at java.base/java.net.URL.<init>(URL.java:515) at com.URL.URL2.main(URL2.java:10)


Some other methods of the URL class

URL(String)This method creates a URL object from the String representation.

URL(URL, String)This method creates a URL by parsing the given spec within a specified context.

URL(String, String, String)This method creates a URL from the specified protocol name, hostname, and file name.

URL(URL, String, URLStreamHandler)This method creates a URL by parsing the given spec with the specified handler within a specified context.

URL(String, String, int, String)This method creates a URL object from the specified protocol, host, port number, and file. host can be expressed as a hostname or a literal IP address.

URL(String, String, int, String, URLStreamHandler)This method creates a URL object from the specified protocol, host, port number, file, and handler.

equals(Object)This method compares this URL for equality with another object.

getAuthority()This method gets the authority part of this URL.

getContent()This method gets the contents of this URL.

getContent(Class<?>[])This method gets the contents of this URL.

getDefaultPort()This method gets the default port number of the protocol associated with this URL.

getFile()This method gets the file name of this URL. 

getHost()This method gets the hostname of this URL, if applicable.

getPath()This method gets the path part of this URL.

getPort()This method gets the port number of this URL.

getProtocol()This method gets the protocol name of this URL.

getQuery()This method gets the query part of this URL.

getRef()This method gets the anchor (also known as the "reference") of this URL.

getUserInfo()This method gets the userInfo part of this URL.

hashCode()This method creates an integer suitable for hash table indexing.

openConnection()This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL.

openConnection(Proxy)This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL with Proxy.

openStream()This method opens a connection to this URL and returns an InputStream for reading from that connection.

sameFile(URL)This method compares two URLs, excluding the fragment component.

URL.setURLStreamHandlerFactory(URLStreamHandlerFactory)This method sets an application's URLStreamHandlerFactory.This method can be called at most once in a given Java VirtualMachine.

toExternalForm()This method constructs a string representation of this URL. 

toString()This method constructs a string representation of this URL. The string is created by calling the toExternalForm method of the stream protocol handler for this object.

toURI()This method returns a java.net.URI equivalent to this URL.

No comments:

Post a Comment