{"id":10425,"date":"2015-08-28T07:15:27","date_gmt":"2015-08-28T11:15:27","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10425"},"modified":"2015-08-24T10:05:59","modified_gmt":"2015-08-24T14:05:59","slug":"the-class-that-actually-gets-the-strings-over-the-network-by-means-of-an-objectinputstream-via-http-tunneling","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10425","title":{"rendered":"The class that actually gets the strings over the network by means of an ObjectInputStream via HTTP tunneling."},"content":{"rendered":"<pre>import java.net.*;\r\nimport java.io.*;\r\n\r\n\/** When this class is built, it returns a value\r\n\u00a0*\u00a0 immediately, but this value returns false for isDone\r\n\u00a0*\u00a0 and null for getQueries. Meanwhile, it starts a Thread\r\n\u00a0*\u00a0 to request an array of query strings from the server,\r\n\u00a0*\u00a0 reading them in one fell swoop by means of an\r\n\u00a0*\u00a0 ObjectInputStream. Once they've all arrived, they\r\n\u00a0*\u00a0 are placed in the location getQueries returns,\r\n\u00a0*\u00a0 and the isDone flag is switched to true.\r\n\u00a0*\u00a0 Used by the ShowQueries applet.\r\n\u00a0* \u00a0\r\n\r\n\r\n\u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition\r\n\u00a0*\u00a0 from Prentice Hall and Sun Microsystems Press,\r\n\u00a0 *\u00a0 May be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class QueryCollection implements Runnable {\r\n\u00a0 private String[] queries;\r\n\u00a0 private String[] tempQueries;\r\n\u00a0 private boolean isDone = false;\r\n\u00a0 private URL dataURL;\r\n\r\n\u00a0 public QueryCollection(String urlSuffix, URL currentPage) {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Only the URL suffix need be supplied, since\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ the rest of the URL is derived from the current page.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String protocol = currentPage.getProtocol();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String host = currentPage.getHost();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 int port = currentPage.getPort();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 dataURL = new URL(protocol, host, port, urlSuffix);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 Thread queryRetriever = new Thread(this);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 queryRetriever.start();\r\n\u00a0\u00a0\u00a0 } catch(MalformedURLException mfe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 isDone = true;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n\r\n\u00a0 public void run() {\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 tempQueries = retrieveQueries();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 queries = tempQueries;\r\n\u00a0\u00a0\u00a0 } catch(IOException ioe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 tempQueries = null;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 queries = null;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 isDone = true;\r\n\u00a0 }\r\n\r\n\u00a0 public String[] getQueries() {\r\n\u00a0\u00a0\u00a0 return(queries);\r\n\u00a0 }\r\n\r\n\u00a0 public boolean isDone() {\r\n\u00a0\u00a0\u00a0 return(isDone);\r\n\u00a0 }\r\n\r\n\u00a0 private String[] retrieveQueries() throws IOException {\r\n\u00a0\u00a0\u00a0 URLConnection connection = dataURL.openConnection();\r\n\u00a0\u00a0\u00a0 \/\/ Make sure browser doesn't cache this URL, since\r\n\u00a0\u00a0\u00a0 \/\/ I want different queries for each request.\r\n\u00a0\u00a0\u00a0 connection.setUseCaches(false);\r\n\u00a0\u00a0\u00a0 \/\/ Use ObjectInputStream so I can read a String[]\r\n\u00a0\u00a0\u00a0 \/\/ all at once.\r\n\u00a0\u00a0\u00a0 ObjectInputStream in =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new ObjectInputStream(connection.getInputStream());\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The return type of readObject is Object, so\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ I need a typecast to the actual type.\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 String[] queryStrings = (String[])in.readObject();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(queryStrings);\r\n\u00a0\u00a0\u00a0 } catch(ClassNotFoundException cnfe) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 return(null);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import java.net.*; import java.io.*; \/** When this class is built, it returns a value \u00a0*\u00a0 immediately, but this value returns false for isDone \u00a0*\u00a0 and null for getQueries. Meanwhile, it starts a Thread \u00a0*\u00a0 to request an array of query strings from the server, \u00a0*\u00a0 reading them in one fell swoop by means of an &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10425\">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-10425","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":27064,"url":"http:\/\/bangla.sitestree.com\/?p=27064","url_meta":{"origin":10425,"position":0},"title":"The class that actually gets the strings over the network by means of an ObjectInputStream via HTTP tunneling. #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics","author":"Author-Check- Article-or-Video","date":"May 9, 2021","format":false,"excerpt":"import java.net.*; import java.io.*; \/** When this class is built, it returns a value * immediately, but this value returns false for isDone * and null for getQueries. Meanwhile, it starts a Thread * to request an array of query strings from the server, * reading them in one fell\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":27016,"url":"http:\/\/bangla.sitestree.com\/?p=27016","url_meta":{"origin":10425,"position":1},"title":"A Frame that lets you draw circles with mouse clicks #Programming Code Examples #Java\/J2EE\/J2ME #AWT Components","author":"Author-Check- Article-or-Video","date":"May 8, 2021","format":false,"excerpt":"SavedFrame.java **************** A Frame that lets you draw circles with mouse clicks \/\/************** import java.awt.*; import java.awt.event.*; import java.io.*; \/** A Frame that lets you draw circles with mouse clicks * and then save the Frame and all circles to disk. * public class SavedFrame extends CloseableFrame implements ActionListener {\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":10339,"url":"http:\/\/bangla.sitestree.com\/?p=10339","url_meta":{"origin":10425,"position":2},"title":"A Frame that lets you draw circles with mouse clicks","author":"","date":"August 26, 2015","format":false,"excerpt":"SavedFrame.java **************** A Frame that lets you draw circles with mouse clicks \/\/************** import java.awt.*; import java.awt.event.*; import java.io.*; \/** A Frame that lets you draw circles with mouse clicks \u00a0*\u00a0 and then save the Frame and all circles to disk. \u00a0* public class SavedFrame extends CloseableFrame \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements ActionListener\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":24457,"url":"http:\/\/bangla.sitestree.com\/?p=24457","url_meta":{"origin":10425,"position":3},"title":"Implement your Own Permission: Pretty Simple Java Code #Root","author":"Author-Check- Article-or-Video","date":"April 10, 2021","format":false,"excerpt":"Implementing Your Own Permission package com.gamedev.games; import java.io.*; import java.security.*; import java.util.Hashtable; import com.scoredev.scores.*; public class ExampleGame { public static void main(String args[]) throws Exception { HighScore hs = new HighScore(\"ExampleGame\"); if (args.length == 0) usage(); if (args[0].equals(\"set\")) { hs.setHighScore(Integer.parseInt(args[1])); } else if (args[0].equals(\"get\")) { System.out.println(\"score = \"+ hs.getHighScore()); }\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":10467,"url":"http:\/\/bangla.sitestree.com\/?p=10467","url_meta":{"origin":10425,"position":4},"title":"FilterTag.java Custom tag that modifies the tag body","author":"","date":"August 28, 2015","format":false,"excerpt":"FilterTag.java Custom tag that modifies the tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import cwp.*; \/** A tag that replaces <, >, \", and & with their HTML \u00a0*\u00a0 character entities (<, >, \", and &). \u00a0*\u00a0 After filtering, arbitrary strings can be placed \u00a0*\u00a0 in either\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":27123,"url":"http:\/\/bangla.sitestree.com\/?p=27123","url_meta":{"origin":10425,"position":5},"title":"FilterTag.java Custom tag that modifies the tag body #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics","author":"Author-Check- Article-or-Video","date":"May 11, 2021","format":false,"excerpt":"FilterTag.java Custom tag that modifies the tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import cwp.*; \/** A tag that replaces <, >, \", and & with their HTML * character entities (<, >, \", and &). * After filtering, arbitrary strings can be placed * in either\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\/10425","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=10425"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10425\/revisions"}],"predecessor-version":[{"id":10426,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10425\/revisions\/10426"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}