{"id":86,"date":"2013-06-28T13:52:56","date_gmt":"2013-06-28T13:52:56","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=86"},"modified":"2013-06-28T13:53:49","modified_gmt":"2013-06-28T13:53:49","slug":"creating-cookie-in-servlet-jav","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/creating-cookie-in-servlet-jav\/","title":{"rendered":"Creating cookie in servlet java"},"content":{"rendered":"<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprotected void doGet(HttpServletRequest request, \r\n                     HttpServletResponse response) \r\n                     throws ServletException, IOException \r\n{\t\r\n   int lngCookieSet    = 0;\r\n   String strCookieVal = &quot;&quot;;\r\n   String cookieName   = &quot;Name&quot;;\r\n\t\r\n   Cookie&#x5B;] cookies = request.getCookies();\r\n\t\t\r\n   if(cookies != null &amp;&amp; cookies.length &gt; 0)\r\n   {\r\n      for(int i=0;i&lt;cookies.length;i++)\r\n      {\r\n        if(cookieName.equals(cookies&#x5B;i].getName()))\r\n        {\r\n          lngCookieSet = 1;\r\n          strCookieVal = cookies&#x5B;i].getValue();\r\n        }\r\n      }\r\n   }\t\r\n\t\t\r\n   if(lngCookieSet == 1)\r\n   {\r\n     PrintWriter prw = response.getWriter();\t\r\n     prw.print(strCookieVal);\r\n   }\r\n   else\r\n   {\r\n     Cookie cook = new Cookie(&quot;Name&quot;, &quot;Mugil&quot;);\r\n     cook.setMaxAge(24*60*60*365);\/\/1 Year\r\n     response.addCookie(cook);\t\r\n   }\r\n}\r\n<\/pre>\n<p><strong>Notes<\/strong><br \/>\n1. check  <strong>cookies != null<\/strong> otherwise the compiler will generate null pointer exception<br \/>\n2. The <strong>new Cookie<\/strong> constructor will take Cookie Name and Value as Parameter.<br \/>\n3. The <strong>addCookie<\/strong> Will add cookie to response Header to set cookie on Client Side<\/p>\n","protected":false},"excerpt":{"rendered":"<p>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int lngCookieSet = 0; String strCookieVal = &quot;&quot;; String cookieName = &quot;Name&quot;; Cookie&#x5B;] cookies = request.getCookies(); if(cookies != null &amp;&amp; cookies.length &gt; 0) { for(int i=0;i&lt;cookies.length;i++) { if(cookieName.equals(cookies&#x5B;i].getName())) { lngCookieSet = 1; strCookieVal = cookies&#x5B;i].getValue(); } } } if(lngCookieSet == 1) { PrintWriter prw =&hellip; <a href=\"https:\/\/codethataint.com\/blog\/creating-cookie-in-servlet-jav\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-servlets"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}