java tutorial - Java url class - java programming - learn java - java basics - java for beginners

Java url class

Learn Java - Java tutorial - Java url class - Java examples - Java programs

  • 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.

Sample Code
click below button to copy the code. By - java tutorial - team
  • A URL contains many information:
    • Protocol: In this case, http is the protocol.
    • Server name or IP Address: In this case, is the server name.
    • Port Number: It is an optional attribute. If we write http// , 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.

Methods of Java URL class

  • The class provides many methods.The methods are,
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.

Sample Code

public class URLDemo{  
public static void main(String[] args){  
URL url=new URL("");  
System.out.println("Protocol: "+url.getProtocol());  
System.out.println("Host Name: "+url.getHost());  
System.out.println("Port Number: "+url.getPort());  
}catch(Exception e){System.out.println(e);}  
click below button to copy the code. By - java tutorial - team


 Protocol: http
       Host Name:
       Port Number: -1
       File Name: /java

Related Searches to Java url class