0% found this document useful (0 votes)
12 views7 pages

Java - URL Class

The document provides an overview of the Java URL class, which represents a Uniform Resource Locator (URL) and includes methods for creating, parsing, and manipulating URLs. It explains the structure of a URL, details the constructors and methods available in the URL class, and includes an example program demonstrating how to use the URL class to extract various components of a URL. The document emphasizes the importance of URLs in identifying online resources and provides practical coding examples.

Uploaded by

codespeedcode
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Java - URL Class

The document provides an overview of the Java URL class, which represents a Uniform Resource Locator (URL) and includes methods for creating, parsing, and manipulating URLs. It explains the structure of a URL, details the constructors and methods available in the URL class, and includes an example program demonstrating how to use the URL class to extract various components of a URL. The document emphasizes the importance of URLs in identifying online resources and provides practical coding examples.

Uploaded by

codespeedcode
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Home Java Java - URL Class

Java - URL Class

What is a URL?
URL stands for Uniform Resource Locator and represents a resource on the World Wide
Web, such as a Webpage or FTP directory.

This section shows you how to write Java programs that communicate with a URL. A URL
can be broken down into parts, as follows −

protocol://host:port/path?query#ref

Examples of protocols include HTTP, HTTPS, FTP, and File. The path is also referred to as
the filename, and the host is also called the authority.

Example

The following is a URL to a web page whose protocol is HTTP −

[Link]

Notice that this URL does not specify a port, in which case the default port for the protocol
is used. With HTTP, the default port is 80.

Java URL Class


The URL class is a part of the [Link] package. URL class represents a Uniform Resource
Locator (URL). Where, URLs are used for identifying online resources (for example:
webpages, images used in the webpages, videos, files, etc.)
The URL class provides several constructors and methods for creating, parsing, and
manipulating URLs (or, URL objects).

URL Class Declaration

public final class URL


extends Object
implements Serializable

URL Class Constructors


The [Link] class represents a URL and has a complete set of methods to
manipulate URL in Java.

The URL class has several constructors for creating URLs, including the following −

[Link]. Constructors & Description

public URL(String protocol, String host, int port, String file) throws
MalformedURLException
1
Creates a URL by putting together the given parts.

public URL(String protocol, String host, int port, String file, URLStreamHandler
handler) throws MalformedURLException
2
Creates a URL by putting together the given parts with the specified handler within a
specified context.

public URL(String protocol, String host, String file) throws


MalformedURLException
3
Identical to the previous constructor, except that the default port for the given protocol is
used.

public URL(String url) throws MalformedURLException


4
Creates a URL from the given String.
public URL(URL context, String url) throws MalformedURLException
5
Creates a URL by parsing together the URL and String arguments.

public URL(URL context, String url, URLStreamHandler handler) throws


MalformedURLException
6
Creates a URL by parsing together the URL and String arguments with the specified
handler within a specified context.

URL Class Methods


The URL class contains many methods for accessing the various parts of the URL being
represented. Some of the methods in the URL class include the following −

[Link]. Method & Description

public equals(Object obj)


1
This method compares this URL for equality with another object.

public String getAuthority()


2
This method returns the authority of the URL.

public Object getContent()


3
This method returns the contents of this URL.

public Object getContent(Class<?>[] classes)


4
This method returns the contents of this URL.

public int getDefaultPort()

5
This method returns the default port for the protocol of the URL.

public String getFile()

6
This method returns the filename of the URL.

7 public String getHost()


This method returns the host of the URL.

public String getPath()


8
This method returns the path of the URL.

public int getPort()


9
This method returns the port of the URL.

public String getProtocol()


10
This method returns the protocol of the URL.

public String getQuery()


11
This method returns the query part of the URL.

public String getRef()


12
This method returns the reference part of the URL.

public String getUserInfo()

13
This method returns the userInfo part of the URL.

public int hashCode()

14
This method creates and return an integer suitable for hash table indexing.

public URLConnection openConnection()

15 This method returns a URLConnection instance that represents a connection to the remote
object referred to by the URL.

public URLConnection openConnection(Proxy proxy)

This method acts as openConnection(), except that the connection will be made through
16
the specified proxy; Protocol handlers that do not support proxing will ignore the proxy
parameter and make a normal connection.
public InputStream openStream()

17 This method opens a connection to this URL and returns an InputStream for reading from
that connection.

public boolean sameFile(URL other)


18
This method compares two URLs, excluding the fragment component.

public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)

19
This method sets an application's URLStreamHandlerFactory.

public String toExternalForm()

20
This method constructs and return a string representation of this URL.

public String toString()

21
This method constructs and return a string representation of this URL.

public String toURI()


22
This method returns a URI equivalent to this URL.

[Link]

Example of URL Class


The following URLDemo program demonstrates the various parts of a URL. A URL is
entered on the command line, and the URLDemo program outputs each part of the given
URL.

// File Name : [Link]


import [Link];
import [Link];
public class URLDemo {

public static void main(String [] args) {


try {
URL url = new URL("[Link]
language=en#j2se");

[Link]("URL is " + [Link]());


[Link]("protocol is " + [Link]());
[Link]("authority is " + [Link]());
[Link]("file name is " + [Link]());
[Link]("host is " + [Link]());
[Link]("path is " + [Link]());
[Link]("port is " + [Link]());
[Link]("default port is " + [Link]());
[Link]("query is " + [Link]());
[Link]("ref is " + [Link]());
} catch (IOException e) {
[Link]();
}
}
}

A sample run of the this program will produce the following result −

Output

URL is [Link]
protocol is https
authority is [Link]
file name is /[Link]?language=en
host is [Link]
path is /[Link]
port is -1
default port is 443
query is language=en
ref is j2se

You might also like