1 / 10

Uniform Resource Locator: URL

Uniform Resource Locator: URL. Hierarchy of Classes Methods of the Class Examples of Using the Class . Hierarchy of Classes. URL Class. Class URL is a description of a resource location on the Internet. Complete URL: http://www.cs.joensuu.fi:1547/~john/pub/index.html

Download Presentation

Uniform Resource Locator: URL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Uniform Resource Locator: URL • Hierarchy of Classes • Methods of the Class • Examples of Using the Class

  2. Hierarchy of Classes

  3. URL Class • Class URL is a description of a resource location on the Internet. • Complete URL: http://www.cs.joensuu.fi:1547/~john/pub/index.html -- protocol : http:// -- host : www.cs.joensuu.fi -- port : 1547 -- path : ~smith/pub/index.html • Java provides a class—java.net.URL—to manipulateURLs.

  4. Methods of URL Class • Main methods of the class URL: • URL(String url): create an object using the string parameter (exception MalformedURLException). • URL(String, String, String): protocol, host, path of the resource. • String toString(): return the URL as a strings. • String getHost(): return the name of the host linked to this URL. • String getProtocol(): return le protocol of this URL. • String getPort(): return the number of the associate port.

  5. Methods of URL Class • InputStream openStream(): realize the connection to the URL previously instantiated. • An InputStream object is returned and permits to retrieve informationspecified into the URL. • If the connection fails, the exception IOExceptionis raised.

  6. Creating a URL Instance The following statement creates a Java URL object: String str = "http://www.sun.com”; try { URL location = new URL(str); } catch(MalformedURLException ex) { }

  7. Example 18.6: Retrieving Remote Files Rather than reading the file from the local system, this example reads the file from a Web server. private void showFile() { URL url = null; try { url = new URL(urlString); InputStream is = url.openStream(); infile = new BufferedReader( new InputStreamReader(is)); } catch (FileNotFoundException e) { ... } catch (IOException e) { ... }

  8. Viewing HTML Pages • Given the URL of the page, a Web browser can view an HTML page—for example, http://www.sun.com. • HTTP (Hypertext Transfer Protocol) is the common standard used for communication between a Webserver and the Internet. You can open a URL andview a Web page in a Java applet.

  9. Example 18.5 Viewing HTML Pages from Java The following figure shows the process by which an applet/application reads the files on the Web server:

  10. Example 18.5 Viewing HTML Pages from Java The following figure shows the process by which an applet/application reads the files on the Web server:

More Related