{"id":10501,"date":"2015-08-29T09:32:24","date_gmt":"2015-08-29T13:32:24","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10501"},"modified":"2015-08-24T10:32:18","modified_gmt":"2015-08-24T14:32:18","slug":"urltest-java-demonstrates-trycatch-blocks","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10501","title":{"rendered":"URLTest.java Demonstrates try\/catch blocks."},"content":{"rendered":"<pre>\/** Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \u00a0\r\n\u00a0*\/\r\n\u00a0\r\n\u00a0\/\/ Further simplified getURL method. \r\n\u00a0\r\n\u00a0public URL getURL() {\r\n\u00a0\u00a0\u00a0 if (url != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(url);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 System.out.print(\"Enter URL: \");\r\n\u00a0\u00a0\u00a0 System.out.flush();\r\n\u00a0\u00a0\u00a0 BufferedReader in = new BufferedReader(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new InputStreamReader(System.in));\r\n\u00a0\u00a0\u00a0 String urlString = null;\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 urlString = in.readLine();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 url = new URL(urlString);\r\n\u00a0\u00a0\u00a0 } catch(MalformedURLException mue) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(urlString + \" is not valid.\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \"Try again.\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 getURL();\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"IOError when reading input: \" + ioe);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ioe.printStackTrace(); \/\/ Can skip return(null) now\r\n\u00a0\u00a0\u00a0 } finally {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(url);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\nimport java.net.*; \/\/ For URL, MalformedURLException\r\nimport java.io.*;\u00a0 \/\/ For BufferedReader\r\n\r\n\/** A small class to demonstrate try\/catch blocks.\r\n\u00a0*\r\n\u00a0*\u00a0 Taken from Core Web Programming from\r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class URLTest {\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 URLTest test = new URLTest();\r\n\u00a0\u00a0\u00a0 test.getURL();\r\n\u00a0\u00a0\u00a0 test.printURL();\r\n\u00a0 }\r\n\r\n\u00a0 private URL url = null;\r\n\r\n\u00a0 \/** Read a string from user and create a URL from it. If\r\n\u00a0\u00a0 *\u00a0 reading fails, give up and report error. If reading\r\n\u00a0\u00a0 *\u00a0 succeeds but URL is illegal, try again.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public URL getURL() {\r\n\u00a0\u00a0\u00a0 if (url != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(url);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 System.out.print(\"Enter URL: \");\r\n\u00a0\u00a0\u00a0 System.out.flush();\r\n\u00a0\u00a0\u00a0 BufferedReader in = new BufferedReader(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new InputStreamReader(System.in));\r\n\u00a0\u00a0\u00a0 String urlString;\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 urlString = in.readLine();\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"IOError when reading input: \" + ioe);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ioe.printStackTrace(); \/\/ Show stack dump.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(null);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 url = new URL(urlString);\r\n\u00a0\u00a0\u00a0 } catch(MalformedURLException mue) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(urlString + \" is not valid.\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Try again.\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 getURL();\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 return(url);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Print info on URL. *\/\r\n\r\n\u00a0 public void printURL() {\r\n\u00a0\u00a0\u00a0 if (url == null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"No URL.\");\r\n\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String protocol = url.getProtocol();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String host = url.getHost();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 int port = url.getPort();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (protocol.equals(\"http\") &amp;&amp; (port == -1)) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 port = 80;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String file = url.getFile();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"Protocol: \" + protocol +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\nHost: \" + host +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\nPort: \" + port +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\nFile: \" + file);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n}\r\n\r\n\/** Taken from Core Web Programming from \r\n\u00a0*\u00a0 Prentice Hall and Sun Microsystems Press,\r\n\u00a0*\u00a0 .\r\n\u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown;\r\n\u00a0*\u00a0 may be freely used or adapted. \u00a0\r\n\u00a0*\/\r\n\u00a0\r\n\u00a0 \/\/ Simplified getURL method.\r\n\u00a0 \r\n\u00a0 public URL getURL() {\r\n\u00a0\u00a0\u00a0 if (url != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(url);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 System.out.print(\"Enter URL: \");\r\n\u00a0\u00a0\u00a0 System.out.flush();\r\n\u00a0\u00a0\u00a0 BufferedReader in = new BufferedReader(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new InputStreamReader(System.in));\r\n\u00a0\u00a0\u00a0 String urlString = null;\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 urlString = in.readLine();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 url = new URL(urlString);\r\n\u00a0\u00a0\u00a0 } catch(MalformedURLException mue) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(urlString + \" is not valid.\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \"Try again.\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 getURL();\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 System.out.println(\"IOError when reading input: \" + ioe);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ioe.printStackTrace(); \/\/ Show stack dump\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(null);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 return(url);\r\n\u00a0 }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0 \u00a0*\/ \u00a0 \u00a0\/\/ Further simplified getURL method. \u00a0 \u00a0public URL getURL() { \u00a0\u00a0\u00a0 if (url != null) { \u00a0\u00a0\u00a0\u00a0\u00a0 return(url); \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10501\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,285],"class_list":["post-10501","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26958,"url":"http:\/\/bangla.sitestree.com\/?p=26958","url_meta":{"origin":10501,"position":0},"title":"URLTest.java Demonstrates try\/catch blocks. #Programming Code Examples #Java\/J2EE\/J2ME #Basic Java","author":"Author-Check- Article-or-Video","date":"May 6, 2021","format":false,"excerpt":"\/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * \u00a9 2001 Marty Hall and Larry Brown; * may be freely used or adapted. *\/ \/\/ Further simplified getURL method. public URL getURL() { if (url != null) { return(url); } System.out.print(\"Enter URL:\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10194,"url":"http:\/\/bangla.sitestree.com\/?p=10194","url_meta":{"origin":10501,"position":1},"title":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.","author":"","date":"August 25, 2015","format":false,"excerpt":"UrlRetriever2.java\u00a0 Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class \u00a0*\u00a0 instead of connecting explicitly to the HTTP server. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26593,"url":"http:\/\/bangla.sitestree.com\/?p=26593","url_meta":{"origin":10501,"position":2},"title":"UrlRetriever2.java  Illustrates how the URL class can simplify communication to an HTTP server. #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class * instead of connecting explicitly to the HTTP server. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10192,"url":"http:\/\/bangla.sitestree.com\/?p=10192","url_meta":{"origin":10501,"position":3},"title":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes","author":"","date":"August 24, 2015","format":false,"excerpt":"UrlRetriever.java\u00a0 Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then \u00a0*\u00a0 passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26591,"url":"http:\/\/bangla.sitestree.com\/?p=26591","url_meta":{"origin":10501,"position":4},"title":"UrlRetriever.java  Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: #Programming Code Examples #Java\/J2EE\/J2ME #Network Programming","author":"Author-Check- Article-or-Video","date":"April 29, 2021","format":false,"excerpt":"UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; \/** This parses the input to get a host, port, and file, then * passes these three values to the UriRetriever\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26489,"url":"http:\/\/bangla.sitestree.com\/?p=26489","url_meta":{"origin":10501,"position":5},"title":"UriRetriever.java  Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: #Programming Code Examples #NULL #Network Programming","author":"Author-Check- Article-or-Video","date":"April 26, 2021","format":false,"excerpt":"UriRetriever.java Accepts a host, port, and file from the command line and retrieves the implied URL from the HTTP server by issuing a GET request. Uses the following classes: import java.net.*; import java.io.*; \/** Retrieve a URL given the host, port, and file as three * separate command-line arguments. A\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10501"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10501\/revisions"}],"predecessor-version":[{"id":10502,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10501\/revisions\/10502"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10501"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}