Program to Identify Protocol, Host, File, Path of a URL in Java

Program to Identify Protocol, Host, File, Path of a URL in Java

     The getProtocol() function is used to return a protocol which is accessing a given URL.
     The getHost() function is used to display the Host name of a given URL.
     The getPort() function is used to display a  Port number.
     The getFile() function is used to display the File name which is present given URL. 
     The getQuery() function is used to display the content which is present after question mark (?) of a given URL.

import java.net.*;
public class Main {
   
    public Main() {
    }
   
    public static void main(String[] args) {
    try
    {      
        URL a=new URL("http://www.google.com?hello%2osachin%20");


        URL a1=new URL("http://www.google.com/adsense");
        String str=a.getProtocol();
        System.out.println("The Protocol is: "+str);
        String str1=a.getHost();
        System.out.println("The Host is: "+str1);
        String str2=a1.getFile();
        System.out.println("The File is: "+str2);
        String str3=a.getQuery();
        System.out.println("The Query is: "+str3);
        String str4=a1.getPath();
        System.out.println("The Path is: "+str4);
        String str5=a1.getRef();
        System.out.println("The Reference is: "+str5);
        int n=a.getPort();
        System.out.println("The Port Number is: "+n);
    }
    catch(Exception e)
    {
    System.out.println(e);}}
   
}

Output

The Protocol is: http
The Host is: www.google.com
The File is: /adsense
The Query is: hello%2osachin%20
The Path is: /adsense
The Reference is: null
The Port Number is: -1
BUILD SUCCESSFUL (total time: 0 seconds)

No comments:

Post a Comment

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...