{"id":10177,"date":"2015-08-20T03:09:58","date_gmt":"2015-08-20T07:09:58","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10177"},"modified":"2015-08-01T09:15:38","modified_gmt":"2015-08-01T13:15:38","slug":"showcookies-java-servlet-that-displays-all-cookies-that-arrived-in-current-request-uses-the-servletutilities-class","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10177","title":{"rendered":"ShowCookies.java Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities class."},"content":{"rendered":"<pre>ShowCookies.java  Servlet that displays all cookies that arrived in current request. Uses the ServletUtilities  class. \r\n\r\npackage cwp;\r\n\r\nimport java.io.*;\r\nimport javax.servlet.*;\r\nimport javax.servlet.http.*;\r\n\r\n\/** Sets six cookies: three that apply only to the current\r\n *  session (regardless of how long that session lasts)\r\n *  and three that persist for an hour (regardless of\r\n *  whether the browser is restarted).\r\n *<\/pre>\n<p>* Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. *\/ public class SetCookies extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { for(int i=0; i&lt;3; i++) { \/\/ Default maxAge is -1, indicating cookie \/\/ applies only to current browsing session. Cookie cookie = new Cookie(&#8220;Session-Cookie-&#8221; + i, &#8220;Cookie-Value-S&#8221; + i); response.addCookie(cookie); cookie = new Cookie(&#8220;Persistent-Cookie-&#8221; + i, &#8220;Cookie-Value-P&#8221; + i); \/\/ Cookie is valid for an hour, regardless of whether \/\/ user quits browser, reboots computer, or whatever. cookie.setMaxAge(3600); response.addCookie(cookie); } response.setContentType(&#8220;text\/html&#8221;); PrintWriter out = response.getWriter(); String title = &#8220;Setting Cookies&#8221;; out.println (ServletUtilities.headWithTitle(title) + &#8220;\\n&#8221; + &#8221;<\/p>\n<h1 align=\"\\&quot;CENTER\\&quot;\">&#8221; + title + &#8220;<\/h1>\n<pre>\\n\" +\r\n       \"There are six cookies associated with this page.\\n\" +\r\n       \"To see them, visit the\\n\" +\r\n       \"<a href=\"http:\/\/www.salearningschool.com\/example_codes\/%5C%22\/servlet\/cwp.ShowCookies%5C%22\">\\n\" +\r\n       \"<code>ShowCookies<\/code> servlet<\/a>.\\n\" +\r\n       \"<\/pre>\n<p>\\n&#8221; + &#8220;Three of the cookies are associated only with the\\n&#8221; + &#8220;current session, while three are persistent.\\n&#8221; + &#8220;Quit the browser, restart, and return to the\\n&#8221; + &#8220;<code>ShowCookies<\/code> servlet to verify that\\n&#8221; + &#8220;the three long-lived ones persist across sessions.\\n&#8221; + &#8220;&#8221;); } } ServletUtilities.java package cwp; import javax.servlet.*; import javax.servlet.http.*; \/** Some simple time savers. Note that most are static methods. *<\/p>\n<p>* Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. *\/ public class ServletUtilities { public static final String DOCTYPE = &#8220;&#8221;; public static String headWithTitle(String title) { return(DOCTYPE + &#8220;\\n&#8221; + &#8220;\\n&#8221; + &#8220;\\n&#8221;); } \/** Read a parameter with the specified name, convert it * to an int, and return it. Return the designated default * value if the parameter doesn&#8217;t exist or if it is an * illegal integer format. *\/ public static int getIntParameter(HttpServletRequest request, String paramName, int defaultValue) { String paramString = request.getParameter(paramName); int paramValue; try { paramValue = Integer.parseInt(paramString); } catch(NumberFormatException nfe) { \/\/ null or bad format paramValue = defaultValue; } return(paramValue); } \/** Given an array of Cookies, a name, and a default value, * this method tries to find the value of the cookie with * the given name. If there is no cookie matching the name * in the array, then the default value is returned instead. *\/ public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) { if (cookies != null) { for(int i=0; i&lt;cookies.length; i++)=&#8221;&#8221; {=&#8221;&#8221; cookie=&#8221;&#8221; if=&#8221;&#8221; (cookiename.equals(cookie.getname()))=&#8221;&#8221; return(cookie.getvalue());=&#8221;&#8221; }=&#8221;&#8221; return(defaultvalue);=&#8221;&#8221; **=&#8221;&#8221; given=&#8221;&#8221; an=&#8221;&#8221; array=&#8221;&#8221; of=&#8221;&#8221; cookies=&#8221;&#8221; and=&#8221;&#8221; a=&#8221;&#8221; name,=&#8221;&#8221; this=&#8221;&#8221; method=&#8221;&#8221; tries=&#8221;&#8221; *=&#8221;&#8221; to=&#8221;&#8221; find=&#8221;&#8221; return=&#8221;&#8221; the=&#8221;&#8221; from=&#8221;&#8221; that=&#8221;&#8221; has=&#8221;&#8221; name.=&#8221;&#8221; there=&#8221;&#8221; is=&#8221;&#8221; no=&#8221;&#8221; matching=&#8221;&#8221; name=&#8221;&#8221; in=&#8221;&#8221; array,=&#8221;&#8221; null=&#8221;&#8221; returned.=&#8221;&#8221; public=&#8221;&#8221; static=&#8221;&#8221; getcookie(cookie[]=&#8221;&#8221; cookies,=&#8221;&#8221; string=&#8221;&#8221; cookiename)=&#8221;&#8221; (cookies=&#8221;&#8221; !=&#8221;null)&#8221; for(int=&#8221;&#8221; i=&#8221;0;&#8221; i&lt;cookies.length;=&#8221;&#8221; return(cookie);=&#8221;&#8221; return(null);=&#8221;&#8221; string,=&#8221;&#8221; replaces=&#8221;&#8221; all=&#8221;&#8221; occurrences=&#8221;&#8221; &#8216;&lt;&#8216;=&#8221;&#8221; with=&#8221;&#8221; &#8216;&lt;&#8216;,=&#8221;&#8221; &#8216;=&#8221;&#8221;&gt;&#8217; with * &#8216;&gt;&#8217;, and (to handle cases that occur inside attribute * values), all occurrences of double quotes with * &#8216;&#8221;&#8216; and all occurrences of &#8216;&amp;&#8217; with &#8216;&amp;&#8217;. * Without such filtering, an arbitrary string * could not safely be inserted in a Web page. *\/ public static String filter(String input) { StringBuffer filtered = new StringBuffer(input.length()); char c; for(int i=0; i&lt;input.length(); i++)=&#8221;&#8221; {=&#8221;&#8221; c=&#8221;input.charAt(i);&#8221; if=&#8221;&#8221; (c=&#8221;=&#8221; &#8216;&lt;&#8216;)=&#8221;&#8221; filtered.append(&#8220;&lt;&#8220;);=&#8221;&#8221; }=&#8221;&#8221; else=&#8221;&#8221; &#8216;=&#8221;&#8221;&gt;&#8217;) { filtered.append(&#8220;&gt;&#8221;); } else if (c == &#8216;&#8221;&#8216;) { filtered.append(&#8220;&#8221;&#8221;); } else if (c == &#8216;&amp;&#8217;) { filtered.append(&#8220;&amp;&#8221;); } else { filtered.append(c); } } return(filtered.toString()); } }<\/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=10177\">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":[1368,308,1457],"class_list":["post-10177","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-cookie","tag-java","tag-servlet","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26551,"url":"http:\/\/bangla.sitestree.com\/?p=26551","url_meta":{"origin":10177,"position":0},"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":[]},{"id":10175,"url":"http:\/\/bangla.sitestree.com\/?p=10175","url_meta":{"origin":10177,"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":10177,"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":10177,"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":10177,"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":10177,"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\/10177","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=10177"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10177\/revisions"}],"predecessor-version":[{"id":10178,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10177\/revisions\/10178"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10177"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}