Prasing URL
Prasing URL
I YEAR II SEM
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 2
URL IN JAVA
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:
4. File Name or directory name: In this case, index.jsp is the file name.
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 3
CONSTRUCTORS OF JAVA URL
CLASS
1. URL(String spec): Creates an instance of a URL from the String representation.
2. URL(String protocol, String host, int port, String file): Creates an instance of a URL from the given protocol, host, port
number, and file.
3. URL(String protocol, String host, int port, String file, URLStreamHandler handler): Creates an instance of a URL
from the given protocol, host, port number, file, and handler.
4. URL(String protocol, String host, String file): Creates an instance of a URL from the given protocol name, host name,
and file name.
5. URL(URL context, String spec): Creates an instance of a URL by parsing the given spec within a specified context.
6. URL(URL context, String spec, URLStreamHandler handler): Creates an instance of a URL by parsing the given spec
with the specified handler within a given context.
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 4
COMMONLY USED METHODS OF JAVA URL CLASS
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 String getAuthority() it returns the authority of the URL.
public String toString() it returns the string representation of the URL.
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 5
EXAMPLE OF JAVA URL CLASS
//URLDemo.java
1.import java.net.*;
2.public class URLDemo{
12.}
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 6
EXAMPLE OF JAVA URL CLASS
1. import java.net.*;
2. public class URLDemo{
3. public static void main(String[] args){
4. try{
5. URL url=new URL("https://www.google.com/search?q=javatpoint&oq=javatpoint&sourceid=chrome&ie=UTF-8");
6.System.out.println("Protocol: "+url.getProtocol()); Output:
7.System.out.println("Host Name: "+url.getHost()); Protocol: https
Host Name: www.google.com
8.System.out.println("Port Number: "+url.getPort()); Port Number: -1
9.System.out.println("Default Port Number: "+url.getDefaultPort()); Default Port Number: 443
Query String:
10.System.out.println("Query String: "+url.getQuery()); q=javatpoint&oq=javatpoint&sourceid=chr
11.System.out.println("Path: "+url.getPath()); ome&ie=UTF-8
Path: /search
12.System.out.println("File: "+url.getFile()); File:
/search?q=javatpoint&oq=javatpoint&sourc
13. }catch(Exception e){System.out.println(e);} } }
eid=chrome&ie=UTF-8
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 7
JAVA URLCONNECTION CLASS
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 8
Reference
1. Herbert Schildt “ The Complete Reference Java 2, 8th edition , Tata McGraw Hill, 2011
2. Ralph Bravaco, Shai Simonson, “Java Programming: From the Ground up Tata McGraw Hill, 2012
3. https://www.javatpoint.com
16.6.23 URL/DR.N.NANDHINI/AP/MCA/SNSCT 9