Java Program to Get Components of a URL
Java Networking: As java programs are written to be executed across multiple devices whereby multiple devices are meant computers present in remote locations. So both devices can communicate be present in the same locations or different locations connected with some network. For java networking ‘net’ package needs to be used. It works on two different protocols namely TCP and UDP. TCP is preferred in java as it is a connection-oriented protocol while UDP is a connectionless protocol. Hence, reliability is less over UDP.
- TCP stands for Transmission Control Protocol
- UDP stands for User Datagram Protocol
Concept: URL Class and inbuilt getProtocol() method
URL Class is a uniform resource locator and it points to a resource on the World Wide Web.
Syntax: for importing URL class:
import java.util.net ;
public String getProtocol() ;
Parameter: This function does not require any parameter
Return Type: The function returns String Type
Illustration: Now, further understanding through illustration the internal working of the URL class:
Considering Random URL: https://www.geeksforgeeks.com/javaexamples/net_singleuser.htm
Protocol Used: http
Host name: www.geeksforgeeks.com
Path is file accessing : javaexaamples/net_singleuser.htm
Port number: 443
Here, HTTP protocol is used as it can easily be seen from the name of the URL itself.
The java.net.URL class represents a URL. There are constructors to make new URLs and methods to parse the various parts of a URL. However, the guts of the category are the methods that allow you to urge an InputStream from a URL so you’ll read data from a server.
The URL class is closely tied to protocol and content handlers. The objective is to separate the info being downloaded from the protocol wont to download it. The protocol handler is liable for communicating with the server, that’s moving bytes from the server to the client. It handles any necessary negotiation with the server and any headers. Its job is to return only the actual bytes of the data or file requested. The content handler takes those bytes and translates them into some quite Java objects like an InputStream or ImageProducer.
Components of a URL: A URL can have many forms. The most general however follows a three-components system-
- Protocol: HTTP is the protocol here
- Hostname: Name of the system of the host.
- File Name: The pathname to the file on the system.
- Port Number: Port number to which to attach (typically optional).
Implementation: Below is the java code to get the parts of a URL:
URL is:https://www.geeksforgeeks.com/javaexamples/net_singleuser.htm protocol is: https file name is: /javaexamples/net_singleuser.htm host is: www.geeksforgeeks.com path is: /javaexamples/net_singleuser.htm port is: -1 default port is: 443
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.