0% found this document useful (0 votes)
13 views

Prasing URL

This document discusses URLs and the Java URL class. It contains: 1) An introduction to URLs, explaining that they contain a protocol, server name/IP address, optional port number, and file name. 2) Details of the Java URL class, including its constructors and commonly used methods to retrieve parts of a URL. 3) An example demonstrating how to create a URL object and retrieve its protocol, host name, port number, and file name.

Uploaded by

Karthick 3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Prasing URL

This document discusses URLs and the Java URL class. It contains: 1) An introduction to URLs, explaining that they contain a protocol, server name/IP address, optional port number, and file name. 2) Details of the Java URL class, including its constructors and commonly used methods to retrieve parts of a URL. 3) An example demonstrating how to create a URL object and retrieve its protocol, host name, port number, and file name.

Uploaded by

Karthick 3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DEPARTMENT OF MCA

I YEAR II SEM

19CAT608 – Java Programming

UNIT III –NETWORKING AND I/O PACKAGES


Topic 16: URL
INTRODUCTION: URL
A domain name is part of a URL, which stands for Uniform Resource
Locator. User can see the visual difference in the following example:

In order for computer networks and servers to “talk to one


another,” computers rely on a language made up of numbers and letters
called an IP address. Every device that connects to the internet has a
unique IP address and looks something like this:
22.231.113.64 or 3ffe:1900:4545:3:200:f8ff:fe21:67cf

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:

1. Protocol: In this case, http is the protocol.


2. Server name or IP Address: In this case, www.google.com is the
server name.
3.Port Number: It is an optional attribute. If we write
http//www.google.com:80/sonoojaiswal/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.

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.

public String getQuery() it returns the query string of the URL.


public String getDefaultPort() it returns the default port of the URL.
public URLConnection it returns the instance of URLConnection i.e. associated with this
openConnection() URL.
public boolean equals(Object obj) it compares the URL with the given object.
public Object getContent() it returns the content of the URL.
public String getRef() it returns the anchor or reference of the URL.
public URI toURI() it returns a URI 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{

3. public static void main(String[] args){


4. try{
5.URL url=new URL("http://www.javatpoint.com/java-tutorial");
6.System.out.println("Protocol: "+url.getProtocol());
7.System.out.println("Host Name: "+url.getHost());
Output:
8.System.out.println("Port Number: "+url.getPort()); Protocol: http
9.System.out.println("File Name: "+url.getFile()); Host Name: www.javatpoint.com
10.}catch(Exception e){System.out.println(e);} Port Number: -1
File Name: /java-tutorial
11.}

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

The Java URLConnection class represents a communication link


between the URL and the application. This class can be used to read and
write data to the specified resource referred by the URL.
1. import java.io.*;
2. import java.net.*;
3. public class URLConnectionExample {
How to get the object of URLConnection class 4. public static void main(String[] args){
5. try{
6.URL url=new URL("http://www.javatpoint.com/java-tutorial");
1.public URLConnection openConnection()throws IOException{} 7.URLConnection urlcon=url.openConnection();
8. InputStream stream=urlcon.getInputStream();
9.int i;
10.while((i=stream.read())!=-1){
11.System.out.print((char)i);
12.}
13.}catch(Exception e){System.out.println(e);}
14.}
15.}

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

You might also like