Advanced JAVA 7
URL class
Java URL Class present in java.net package, deals with URL (Uniform Resource Locator) which uniquely identify or locate resources on internet.
http://www.bethedeveloper.com:18/index.html
protocol : http
hostname : www.bethedeveloper.com
portnumber : 80
file name : index.html
Program using URL class
1. getProtocol ( ) : Returns protocol of URL.
2. getHost( ) : Returns hostname(domain name) of URL.
3. getPort( ) : Returns port number of URL.
4. getFile( ) : Returns filename of URL
Program using URL class
import java.net.*;
class Test
{
public static void main(String[] arg) throws MalformedURLException
{
URL url = new URL("http://www.bethedeveloper.com/java");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}
}
Output:
Protocol: http
Host Name: www.bethedeveloper.com
Port Number: -1
File Name: /java
Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.
Share this
Learn-JAVA