Home | | Web Programming | URL - Java Networking

Chapter: Java The Complete Reference : The Java Library : Networking

URL - Java Networking

The preceding example was rather obscure because the modern Internet is not about the older protocols such as whois, finger, and FTP. It is about WWW, the World Wide Web.

URL

 

The preceding example was rather obscure because the modern Internet is not about the older protocols such as whois, finger, and FTP. It is about WWW, the World Wide Web. The Web is a loose collection of higher-level protocols and file formats, all unified in a web browser. One of the most important aspects of the Web is that Tim Berners-Lee devised a scalable way to locate all of the resources of the Net. Once you can reliably name anything and everything, it becomes a very powerful paradigm. The Uniform Resource Locator (URL) does exactly that.

 

The URL provides a reasonably intelligible form to uniquely identify or address information on the Internet. URLs are ubiquitous; every browser uses them to identify information on the Web. Within Java’s network class library, the URL class provides a simple, concise API to access information across the Internet using URLs.

All URLs share the same basic format, although some variation is allowed. Here are two examples: http://www.MHProfessional.com/ and http://www.MHProfessional.com:80/ index.htm. A URL specification is based on four components. The first is the protocol to use, separated from the rest of the locator by a colon (:). Common protocols are HTTP, FTP, gopher, and file, although these days almost everything is being done via HTTP (in fact, most browsers will proceed correctly if you leave off the "http://" from your URL specification). The second component is the host name or IP address of the host to use; this is delimited on the left by double slashes (//) and on the right by a slash (/) or optionally a colon (:). The third component, the port number, is an optional parameter, delimited on the left from the host name by a colon (:) and on the right by a slash (/). (It defaults to port 80, the predefined HTTP port; thus, ":80" is redundant.) The fourth part is the actual file path. Most HTTP servers will append a file named index.html or index.htm to URLs that refer directly to a directory resource. Thus, http://www.MHProfessional.com/ is the same as http://www.MHProfessional.com/index.htm.

 

Java’s URL class has several constructors; each can throw a MalformedURLException. One commonly used form specifies the URL with a string that is identical to what you see displayed in a browser:

 

URL(String urlSpecifier) throws MalformedURLException

 

The next two forms of the constructor allow you to break up the URL into its component parts:

 

URL(String protocolName, String hostName, int port, String path ) throws MalformedURLException

tring path) throws MalformedURLException

Another frequently used constructor allows you to use an existing URL as a reference context and then create a new URL from that context. Although this sounds a little contorted, it’s really quite easy and useful.

 

URL(URL urlObj, String urlSpecifier) throws MalformedURLException

 

The following example creates a URL to a page on HerbSchildt.com and then examines its properties:

 

// Demonstrate URL.

import java.net.*; class URLDemo {

 

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

URL hp = new URL(http://www.HerbSchildt.com/WhatsNew");

 

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

 

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

 

System.out.println("Host: " + hp.getHost()); System.out.println("File: " + hp.getFile()); System.out.println("Ext:" + hp.toExternalForm());

 

}

 

}

 

When you run this, you will get the following output:

 

Protocol: http

 

Port: -1

 

Host:          www.HerbSchildt.com

 

File: /WhatsNew

 

Ext:http://www.HerbSchildt.com/WhatsNew

 

Notice that the port is –1; this means that a port was not explicitly set. Given a URL object, you can retrieve the data associated with it. To access the actual bits or content information of a URL, create a URLConnection object from it, using its openConnection( ) method, like this:

 

 

urlc = url.openConnection()

 

openConnection( ) has the following general form: URLConnection openConnection( ) throws IOException

 

It returns a URLConnection object associated with the invoking URL object. Notice that it may throw an IOException.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Networking : URL - Java Networking |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.