{"id":26521,"date":"2021-04-27T23:10:04","date_gmt":"2021-04-28T03:10:04","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/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-ja\/"},"modified":"2021-04-27T23:10:04","modified_gmt":"2021-04-28T03:10:04","slug":"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-ja","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26521","title":{"rendered":"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"},"content":{"rendered":"<pre>\nServletUtilities.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. \n\npackage cwp;\n\nimport javax.servlet.*;\nimport javax.servlet.http.*;\n\n\/** Some simple time savers. Note that most are static methods.\n *  <p>\n *  Taken from Core Web Programming Java 2 Edition\n *  from Prentice Hall and Sun Microsystems Press,\n *  .\n *  May be freely used or adapted.\n *\/\n\npublic class ServletUtilities {\n  public static final String DOCTYPE =\n    &quot;&quot;;\n\n  public static String headWithTitle(String title) {\n    return(DOCTYPE + &quot;n&quot; +\n           &quot;n&quot; +\n           &quot;<title>&quot; + title + &quot;<\/title>n&quot;);\n  }\n\n  \/** Read a parameter with the specified name, convert it\n   *  to an int, and return it. Return the designated default\n   *  value if the parameter doesn't exist or if it is an\n   *  illegal integer format.\n  *\/\n\n  public static int getIntParameter(HttpServletRequest request,\n                                    String paramName,\n                                    int defaultValue) {\n    String paramString = request.getParameter(paramName);\n    int paramValue;\n    try {\n      paramValue = Integer.parseInt(paramString);\n    } catch(NumberFormatException nfe) { \/\/ null or bad format\n      paramValue = defaultValue;\n    }\n    return(paramValue);\n  }\n\n  \/** Given an array of Cookies, a name, and a default value,\n   *  this method tries to find the value of the cookie with\n   *  the given name. If there is no cookie matching the name\n   *  in the array, then the default value is returned instead.\n   *\/\n\n  public static String getCookieValue(Cookie[] cookies,\n                                      String cookieName,\n                                      String defaultValue) {\n    if (cookies != null) {\n      for(int i=0; i&lt;cookies .length; i++) {\n        Cookie cookie = cookies[i];\n        if (cookieName.equals(cookie.getName()))\n          return(cookie.getValue());\n      }\n    }\n    return(defaultValue);\n  }\n\n  \/** Given an array of cookies and a name, this method tries\n   *  to find and return the cookie from the array that has\n   *  the given name. If there is no cookie matching the name\n   *  in the array, null is returned.\n   *\/\n\n  public static Cookie getCookie(Cookie[] cookies,\n                                 String cookieName) {\n    if (cookies != null) {\n      for(int i=0; i&lt;cookies.length; i++) {\n        Cookie cookie = cookies[i];\n        if (cookieName.equals(cookie.getName()))\n          return(cookie);\n      }\n    }\n    return(null);\n  }\n\n  \/** Given a string, this method replaces all occurrences of\n   *  &#039;' with\n   *  '&gt;', and (to handle cases that occur inside attribute\n   *  values), all occurrences of double quotes with\n   *  '&quot;' and all occurrences of '&amp;' with '&amp;'.\n   *  Without such filtering, an arbitrary string\n   *  could not safely be inserted in a Web page.\n   *\/\n\n  public static String filter(String input) {\n    StringBuffer filtered = new StringBuffer(input.length());\n    char c;\n    for(int i=0; i&lt;input .length(); i++) {\n      c = input.charAt(i);\n      if (c == &#039;') {\n        filtered.append(&quot;&gt;&quot;);\n      } else if (c == '&quot;') {\n        filtered.append(&quot;&quot;&quot;);\n      } else if (c == '&amp;') {\n        filtered.append(&quot;&amp;&quot;);\n      } else {\n        filtered.append(c);\n      }\n    }\n    return(filtered.toString());\n  }\n}\n\n\n\n<\/p><\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10243<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Servlet<br \/>Tags:Java\/J2EE\/J2MEServlet<br \/> Post Data:2017-01-02 16:04:28<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 2 Edition * from Prentice &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26521\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26521","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10156,"url":"http:\/\/bangla.sitestree.com\/?p=10156","url_meta":{"origin":26521,"position":0},"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":10154,"url":"http:\/\/bangla.sitestree.com\/?p=10154","url_meta":{"origin":26521,"position":1},"title":"HelloWWW.java Servlet that generates HTML. This and all remaining servlets are in the cwp package and therefore should be installed in the cwp subdirectory.","author":"","date":"August 14, 2015","format":false,"excerpt":"HelloWWW.java\u00a0 Servlet that generates HTML. This and all remaining servlets are in the cwp package and therefore should be installed in the cwp subdirectory. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that generates HTML. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0\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":26519,"url":"http:\/\/bangla.sitestree.com\/?p=26519","url_meta":{"origin":26521,"position":2},"title":"HelloWWW.java  Servlet that generates HTML. This and all remaining servlets are in the cwp package and therefore should be installed in the cwp subdirectory. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"HelloWWW.java Servlet that generates HTML. This and all remaining servlets are in the cwp package and therefore should be installed in the cwp subdirectory. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that generates HTML. * * Taken from Core Web Programming Java 2 Edition * from\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":10164,"url":"http:\/\/bangla.sitestree.com\/?p=10164","url_meta":{"origin":26521,"position":3},"title":"ServletUtilities.java Utility class that, among other things, contains the static filter method that replaces special HTML characters with their HTML character entities.","author":"","date":"August 17, 2015","format":false,"excerpt":"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\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":26521,"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. #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":26529,"url":"http:\/\/bangla.sitestree.com\/?p=26529","url_meta":{"origin":26521,"position":5},"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":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26521","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26521"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26521\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26521"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}