{"id":26551,"date":"2021-04-28T23:10:06","date_gmt":"2021-04-29T03:10:06","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/showcookies-java-servlet-that-displays-all-cookies-that-arrived-in-current-request-uses-the-servletutilities-class-programming-code-examples-java-j2ee-j2me-servlet\/"},"modified":"2021-04-28T23:10:06","modified_gmt":"2021-04-29T03:10:06","slug":"showcookies-java-servlet-that-displays-all-cookies-that-arrived-in-current-request-uses-the-servletutilities-class-programming-code-examples-java-j2ee-j2me-servlet","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26551","title":{"rendered":"ShowCookies.java  Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities  class. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet"},"content":{"rendered":"<pre>\n\nShowCookies.java  Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities  class. \n\npackage cwp;\n\nimport java.io.*;\nimport javax.servlet.*;\nimport javax.servlet.http.*;\n\n\/** Sets six cookies: three that apply only to the current\n *  session (regardless of how long that session lasts)\n *  and three that persist for an hour (regardless of\n *  whether the browser is restarted).\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 SetCookies extends HttpServlet {\n  public void doGet(HttpServletRequest request,\n                    HttpServletResponse response)\n      throws ServletException, IOException {\n    for(int i=0; i&lt;3; i++) {\n      \/\/ Default maxAge is -1, indicating cookie\n      \/\/ applies only to current browsing session.\n      Cookie cookie = new Cookie(&quot;Session-Cookie-&quot; + i,\n                                 &quot;Cookie-Value-S&quot; + i);\n      response.addCookie(cookie);\n      cookie = new Cookie(&quot;Persistent-Cookie-&quot; + i,\n                          &quot;Cookie-Value-P&quot; + i);\n      \/\/ Cookie is valid for an hour, regardless of whether\n      \/\/ user quits browser, reboots computer, or whatever.\n      cookie.setMaxAge(3600);\n      response.addCookie(cookie);\n    }\n    response.setContentType(&quot;text\/html&quot;);\n    PrintWriter out = response.getWriter();\n    String title = &quot;Setting Cookies&quot;;\n    out.println\n      (ServletUtilities.headWithTitle(title) +\n       &quot;n&quot; +\n       &quot;<h1 ALIGN=\"&quot;CENTER&quot;\">&quot; + title + &quot;<\/h1>n&quot; +\n       &quot;There are six cookies associated with this page.n&quot; +\n       &quot;To see them, visit then&quot; +\n       &quot;<a HREF=\"&quot;\/servlet\/cwp.ShowCookies&quot;\">n&quot; +\n       &quot;<code>ShowCookies<\/code> servlet<\/a>.n&quot; +\n       &quot;<\/p><p>n&quot; +\n       &quot;Three of the cookies are associated only with then&quot; +\n       &quot;current session, while three are persistent.n&quot; +\n       &quot;Quit the browser, restart, and return to then&quot; +\n       &quot;<code>ShowCookies<\/code> servlet to verify thatn&quot; +\n       &quot;the three long-lived ones persist across sessions.n&quot; +\n       &quot;<\/p>&quot;);\n  }\n}\n\n\nServletUtilities.java\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=10253<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>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 hour (regardless of * whether &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26551\">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-26551","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":10177,"url":"http:\/\/bangla.sitestree.com\/?p=10177","url_meta":{"origin":26551,"position":0},"title":"ShowCookies.java Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities class.","author":"","date":"August 20, 2015","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;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":10175,"url":"http:\/\/bangla.sitestree.com\/?p=10175","url_meta":{"origin":26551,"position":1},"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":26549,"url":"http:\/\/bangla.sitestree.com\/?p=26549","url_meta":{"origin":26551,"position":2},"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":10162,"url":"http:\/\/bangla.sitestree.com\/?p=10162","url_meta":{"origin":26551,"position":3},"title":"ThreeParams.java Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities class.","author":"","date":"August 16, 2015","format":false,"excerpt":"ThreeParams.java\u00a0 Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities\u00a0 class. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that reads three parameters from the \u00a0*\u00a0 form data. \u00a0* \u00a0 \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0 from Prentice Hall and\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":10158,"url":"http:\/\/bangla.sitestree.com\/?p=10158","url_meta":{"origin":26551,"position":4},"title":"SimplerHelloWWW.java Servlet that uses ServletUtilities to simplify the generation of the DOCTYPE and HEAD part of the servlet.","author":"","date":"August 15, 2015","format":false,"excerpt":"SimplerHelloWWW.java\u00a0 Servlet that uses ServletUtilities\u00a0 to simplify the generation of the DOCTYPE and HEAD part of the servlet. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that generates HTML. This variation of \u00a0*\u00a0 HelloWWW uses the ServletUtilities utility class \u00a0*\u00a0 to generate the DOCTYPE, HEAD, and TITLE.\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":26527,"url":"http:\/\/bangla.sitestree.com\/?p=26527","url_meta":{"origin":26551,"position":5},"title":"ThreeParams.java  Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities  class. #Programming Code Examples #Java\/J2EE\/J2ME #Servlet","author":"Author-Check- Article-or-Video","date":"April 27, 2021","format":false,"excerpt":"ThreeParams.java Servlet that reads and displays three request (form) parameters. Uses the ServletUtilities class. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; \/** Simple servlet that reads three parameters from the * form data. * * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun\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\/26551","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=26551"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26551\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26551"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}