{"id":10164,"date":"2015-08-17T00:31:28","date_gmt":"2015-08-17T04:31:28","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10164"},"modified":"2015-08-01T09:26:31","modified_gmt":"2015-08-01T13:26:31","slug":"servletutilities-java-utility-class-that-among-other-things-contains-the-static-filter-method-that-replaces-special-html-characters-with-their-html-character-entities","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10164","title":{"rendered":"ServletUtilities.java Utility class that, among other things, contains the static filter method that replaces special HTML characters with their HTML character entities."},"content":{"rendered":"<pre>ServletUtilities.java\u00a0 Utility class that, among other things, contains the static filter\u00a0 method that replaces special HTML characters with their HTML character entities. \r\n\r\npackage cwp;\r\n\r\nimport javax.servlet.*;\r\nimport javax.servlet.http.*;\r\n\r\n\/** Some simple time savers. Note that most are static methods.\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 .\r\n\u00a0*\u00a0 May be freely used or adapted.\r\n\u00a0*\/\r\n\r\npublic class ServletUtilities {\r\n\u00a0 public static final String DOCTYPE =\r\n\u00a0\u00a0\u00a0 \"\";\r\n\r\n\u00a0 public static String headWithTitle(String title) {\r\n\u00a0\u00a0\u00a0 return(DOCTYPE + \"\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\n\" +\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"\\n\");\r\n\u00a0 }\r\n\r\n\u00a0 \/** Read a parameter with the specified name, convert it\r\n\u00a0\u00a0 *\u00a0 to an int, and return it. Return the designated default\r\n\u00a0\u00a0 *\u00a0 value if the parameter doesn't exist or if it is an\r\n\u00a0\u00a0 *\u00a0 illegal integer format.\r\n\u00a0 *\/\r\n\r\n\u00a0 public static int getIntParameter(HttpServletRequest request,\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\u00a0\u00a0\u00a0\u00a0 String paramName,\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\u00a0\u00a0\u00a0\u00a0 int defaultValue) {\r\n\u00a0\u00a0\u00a0 String paramString = request.getParameter(paramName);\r\n\u00a0\u00a0\u00a0 int paramValue;\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 paramValue = Integer.parseInt(paramString);\r\n\u00a0\u00a0\u00a0 } catch(NumberFormatException nfe) { \/\/ null or bad format\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 paramValue = defaultValue;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 return(paramValue);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Given an array of Cookies, a name, and a default value,\r\n\u00a0\u00a0 *\u00a0 this method tries to find the value of the cookie with\r\n\u00a0\u00a0 *\u00a0 the given name. If there is no cookie matching the name\r\n\u00a0\u00a0 *\u00a0 in the array, then the default value is returned instead.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static String getCookieValue(Cookie[] cookies,\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String cookieName,\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\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 String defaultValue) {\r\n\u00a0\u00a0\u00a0 if (cookies != null) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int i=0; i' with\r\n\u00a0\u00a0 *\u00a0 '&gt;', and (to handle cases that occur inside attribute\r\n\u00a0\u00a0 *\u00a0 values), all occurrences of double quotes with\r\n\u00a0\u00a0 *\u00a0 '\"' and all occurrences of '&amp;' with '&amp;'.\r\n\u00a0\u00a0 *\u00a0 Without such filtering, an arbitrary string\r\n\u00a0\u00a0 *\u00a0 could not safely be inserted in a Web page.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static String filter(String input) {\r\n\u00a0\u00a0\u00a0 StringBuffer filtered = new StringBuffer(input.length());\r\n\u00a0\u00a0\u00a0 char c;\r\n\u00a0\u00a0\u00a0 for(int i=0; i') {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filtered.append(\"&gt;\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 } else if (c == '\"') {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filtered.append(\"\"\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 } else if (c == '&amp;') {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filtered.append(\"&amp;\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 filtered.append(c);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 return(filtered.toString());\r\n\u00a0 }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ServletUtilities.java\u00a0 Utility class that, among other things, contains the static filter\u00a0 method that replaces special HTML characters with their HTML character entities. package cwp; import javax.servlet.*; import javax.servlet.http.*; \/** Some simple time savers. Note that most are static methods. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0 from Prentice Hall and &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10164\">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,1457,1458,285],"class_list":["post-10164","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-servlet","tag-utilitie","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26529,"url":"http:\/\/bangla.sitestree.com\/?p=26529","url_meta":{"origin":10164,"position":0},"title":"ServletUtilities.java  Utility class that, among other things, contains the static filter  method that replaces special HTML characters with their HTML character entities. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"ServletUtilities.java Utility class that, among other things, contains the static filter method that replaces special HTML characters with their HTML character entities. package cwp; import javax.servlet.*; import javax.servlet.http.*; \/** Some simple time savers. Note that most are static methods. * * Taken from Core Web Programming Java 2 Edition *\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":26521,"url":"http:\/\/bangla.sitestree.com\/?p=26521","url_meta":{"origin":10164,"position":1},"title":"ServletUtilities.java  Utility class that simplifies the output of the DOCTYPE and HEAD  in servlets, among other things. Used by most remaining servlets in the chapter. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"ServletUtilities.java Utility class that simplifies the output of the DOCTYPE and HEAD in servlets, among other things. Used by most remaining servlets in the chapter. package cwp; import javax.servlet.*; import javax.servlet.http.*; \/** Some simple time savers. Note that most are static methods. * * Taken from Core Web Programming Java\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":10156,"url":"http:\/\/bangla.sitestree.com\/?p=10156","url_meta":{"origin":10164,"position":2},"title":"ServletUtilities.java Utility class that simplifies the output of the DOCTYPE and HEAD in servlets, among other things. Used by most remaining servlets in the chapter.","author":"","date":"August 15, 2015","format":false,"excerpt":"ServletUtilities.java\u00a0 Utility class that simplifies the output of the DOCTYPE and HEAD\u00a0 in servlets, among other things. Used by most remaining servlets in the chapter. package cwp; import javax.servlet.*; import javax.servlet.http.*; \/** Some simple time savers. Note that most are static methods. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming\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":26549,"url":"http:\/\/bangla.sitestree.com\/?p=26549","url_meta":{"origin":10164,"position":3},"title":"SetCookies.java  Servlet that sets a few persistent and session cookies. Uses the ServletUtilities  class to simplify the DOCTYPE and HEAD output. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"SetCookies.java Servlet that sets a few persistent and session cookies. Uses the ServletUtilities class to simplify the DOCTYPE and HEAD output. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Sets six cookies: three that apply only to the current * session (regardless of how long that session lasts) *\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":10175,"url":"http:\/\/bangla.sitestree.com\/?p=10175","url_meta":{"origin":10164,"position":4},"title":"SetCookies.java Servlet that sets a few persistent and session cookies. Uses the ServletUtilities class to simplify the DOCTYPE and HEAD output.","author":"","date":"August 19, 2015","format":false,"excerpt":"SetCookies.java\u00a0 Servlet that sets a few persistent and session cookies. Uses the ServletUtilities\u00a0 class to simplify the DOCTYPE and HEAD output. \u00a0 package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Sets six cookies: three that apply only to the current \u00a0*\u00a0 session (regardless of how long that session lasts)\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":26551,"url":"http:\/\/bangla.sitestree.com\/?p=26551","url_meta":{"origin":10164,"position":5},"title":"ShowCookies.java  Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities  class. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"ShowCookies.java Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities class. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Sets six cookies: three that apply only to the current * session (regardless of how long that session lasts) * and three that persist for an\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\/10164","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=10164"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10164\/revisions"}],"predecessor-version":[{"id":10165,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10164\/revisions\/10165"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}