The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a resource on the World Wide Web. For example:
A URL contains many information:
- Protocol: In this case, http is the protocol.
- Server name or IP Address: In this case, www.kundansingh0510.blogspot.in is the server name.
- Port Number: It is an optional attribute. If we write http//www.kundansingh0510.blogspot.in:80/kundansinghl/ , 80 is the port number. If port number is not mentioned in the URL, it returns -1.
- File Name or directory name: In this case, index.jsp is the file name.
Commonly used methods of Java URL class
The java.net.URL class provides many methods. The important methods of URL class are given below.
Method | Description |
---|---|
public String getProtocol() | it returns the protocol of the URL. |
public String getHost() | it returns the host name of the URL. |
public String getPort() | it returns the Port Number of the URL. |
public String getFile() | it returns the file name of the URL. |
public URLConnection openConnection() | it returns the instance of URLConnection i.e. associated with this URL. |
Example of Java URL class
Output:
Protocol: http
Host Name: www.kundansingh0510.blogspot.in
Port Number: -1
File Name: /java-tutorial
No comments:
Post a Comment