The getProtocol
method of the URL
class can be used to get the protocol (scheme) of the URL.
public String getProtocol()
This method doesn’t take any arguments. The return type of this method is a string.
The
URL
class is present in thejava.net
package.
In the image above, the part labeled with the scheme denotes the protocol of the URL.
The code below uses the getProtocol
method.
import java.net.URL;class GetRefExample {public static void main( String args[] ) {try {URL url= new URL("https://www.educative.io");//Get ProtocolString protocol=url.getProtocol();System.out.println("The URL is : "+url);System.out.println("The protocol is : "+protocol);} catch (Exception e) {System.out.println(e);}}}
In the code above:
URL
object.URL url= new URL("https://www.educative.io");
getProtocol
method to get the protocol of the URL
object.String protocol=url.getProtocol();
url
and protocol
of the URL
.