{"id":698,"date":"2013-03-26T07:00:13","date_gmt":"2013-03-26T07:00:13","guid":{"rendered":"http:\/\/codeatelier.wordpress.com\/?p=493"},"modified":"2013-03-26T07:00:13","modified_gmt":"2013-03-26T07:00:13","slug":"creating-cookies-in-servlets","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/creating-cookies-in-servlets\/","title":{"rendered":"Creating Cookies in Servlets"},"content":{"rendered":"<pre>\nprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException \n{\t\n\tint lngCookieSet    = 0;\n\tString strCookieVal = \"\";\n\t\n\tString cookieName = \"Name\";\n\n\tCookie[] cookies = request.getCookies();\n\t\t\n\tif(cookies != null &amp;&amp; cookies.length &gt; 0)\n\t{\n\t  for(int i=0;i&lt;cookies.length;i++)\n\t  {\n\t    if(cookieName.equals(cookies[i].getName()))\n\t    {\n\t      lngCookieSet = 1;\n\t      strCookieVal = cookies[i].getValue();\n\t    }\n\t  }\n\t}\t\n\t\t\n\tif(lngCookieSet == 1)\n\t{\n\t   PrintWriter prw = response.getWriter();\t\n\t   prw.print(strCookieVal);\n\t}\n\telse\n\t{\n\t   Cookie cook = new Cookie(&quot;Name&quot;, &quot;Mugil&quot;);\n\t   cook.setMaxAge(24*60*60*365);\/\/1 Year\n\t   response.addCookie(cook);\t\n\t}\n}\n<\/pre>\n<p>1.We should check <\/p>\n<pre>cookies != null<\/pre>\n<p> otherwise the compiler will generate null pointer exception<\/p>\n<p>2.The <\/p>\n<pre>new Cookie<\/pre>\n<p> constructor will take Cookie Name and Value as Parameter.<\/p>\n<p>3.The addCookie 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 = &#8220;&#8221;; String cookieName = &#8220;Name&#8221;; Cookie[] 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[i].getName())) { lngCookieSet = 1; strCookieVal = cookies[i].getValue(); } } } if(lngCookieSet == 1) { PrintWriter prw =&hellip; <a href=\"https:\/\/codethataint.com\/blog\/creating-cookies-in-servlets\/\">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":[53],"tags":[],"class_list":["post-698","post","type-post","status-publish","format-standard","hentry","category-jsp"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/698","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=698"}],"version-history":[{"count":0,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/698\/revisions"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}