Posted By:
18 Feb, 2014 9:39 am PST

To retrieve IP address of a website and different parts of URL

Write a program to accept a website name and return its IP Address, after checking it on Internet.

Note: This program should be executed on a system which is connected to Internet.

//Knowing IPAddress of a website



public class Address {

 public static void main(String[] args) throws IOException{

 //accept name of website from keyboard

BufferedReader br = new BufferedReader(new InputStreamReader(;

 System.out.print("Enter a website name: ");

String site=br.readLine();


 //getByName() method accepts site name and returns its IPAddress

InetAddress ip= InetAddress.getByName(site);

System.out.println("The IP Address is: "+ ip);


catch(UnknownHostException ue){

 System.out.println("Website not found");





Write a program to retrieve different parts of a URL supplied to URL class object.



public class MyURL {

public static void main(String[] args) throws Exception {

URL ur=new URL("");

System.out.println("Protocol: "+ ur.getProtocol());

System.out.println("Host: "+ ur.getHost());

System.out.println("File: " + ur. getFile());

System.out.println("Port: "+ ur.getPort());

System.out.println("Path: "+ ur.getFile());

System.out.println("External Form: "+ ur.toExternalForm());




Note: Please note that port number is displayed as -1 in the output, which represents that no port number is passed in the URL.


Video is posted for better understanding of the concept.

Was it useful?  

Please login to reply to this problem.