URL Class in JAVA

11,631 views

Published on

Presentation

Published in: Education, Technology, Design
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
11,631
On SlideShare
0
From Embeds
0
Number of Embeds
138
Actions
Shares
0
Downloads
168
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

URL Class in JAVA

  1. 1. URL Class In JAVA
  2. 2. Uniform Resource Locator: URL <ul><li>Hierarchy of Classes </li></ul><ul><li>Methods of the Class </li></ul><ul><li>Examples of Using the Class </li></ul>
  3. 3. Hierarchy of Classes
  4. 4. URL Class <ul><li>Class URL is a description of a resource location on the Internet. </li></ul><ul><li>Complete URL: http://www.cs.joensuu.fi:1547/~john/pub/index.html </li></ul><ul><li> -- protocol : http:// </li></ul><ul><li> -- host : www.cs.joensuu.fi </li></ul><ul><li>-- port : 1547 </li></ul><ul><li>-- path : ~smith/pub/index.html </li></ul><ul><li>Java provides a class— java.net.URL —to manipulate URLs. </li></ul>
  5. 5. Methods of URL Class <ul><li>Main methods of the class URL: </li></ul><ul><li>URL(String url) : create an object using the string parameter (exception MalformedURLException ). </li></ul><ul><li>URL(String, String, String) : protocol, host, path of the resource. </li></ul><ul><li>String toString() : return the URL as a strings. </li></ul><ul><li>String getHost() : return the name of the host linked to this URL. </li></ul><ul><li>String getProtocol() : return le protocol of this URL. </li></ul><ul><li>String getPort() : return the number of the associate port. </li></ul>
  6. 6. Methods of URL Class <ul><li>InputStream openStream(): realize the connection to the URL previously instantiated. </li></ul><ul><li>An InputStream object is returned and permits to retrieve information specified into the UR L. </li></ul><ul><li>If the connection fails, the exception IOException is raised. </li></ul>
  7. 7. Creating a URL Instance <ul><li>The following statement creates a Java URL object: </li></ul><ul><li>String str = &quot;http://www.sun.com”; </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>URL location = new URL(str); </li></ul><ul><li>} </li></ul><ul><li>catch(MalformedURLException ex) </li></ul><ul><li>{ } </li></ul>
  8. 8. Example 18.6: Retrieving Remote Files <ul><li>Rather than reading the file from the local system, this example </li></ul><ul><li>reads the file from a Web server. </li></ul><ul><li>private void showFile() { </li></ul><ul><li>URL url = null; </li></ul><ul><li>try { </li></ul><ul><li>url = new URL(urlString); </li></ul><ul><li>InputStream is = url.openStream(); </li></ul><ul><li>infile = new BufferedReader( </li></ul><ul><li>new InputStreamReader(is)); </li></ul><ul><li>} </li></ul><ul><li>catch (FileNotFoundException e) { ... } </li></ul><ul><li>catch (IOException e) { ... } </li></ul>
  9. 9. Viewing HTML Pages <ul><li>Given the URL of the page, a Web browser can view an HTML page—for example, http://www.sun.com . </li></ul><ul><li>HTTP (Hypertext Transfer Protocol) is the common standard used for communication between a Web server and the Internet. You can open a URL and view a Web page in a Java applet. </li></ul>
  10. 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:
  11. 11. 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:

×