

{"id":81357,"date":"2020-08-31T09:00:34","date_gmt":"2020-08-31T03:30:34","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=81357"},"modified":"2021-08-25T13:47:34","modified_gmt":"2021-08-25T08:17:34","slug":"cookies-handling-in-jsp","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/","title":{"rendered":"JSP Cookies Handling with Examples"},"content":{"rendered":"<p>This article discusses the cookies handling in JSP in detail. It discusses cookies, its types, working, structure. It also provides a description of setting the cookie, reading the cookies and deleting the cookies in JSP. So let&#8217;s start our journey of Cookies handling in JSP.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81431\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg\" alt=\"Cookies Handling in JSP\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>JSP Cookies Handling<\/h2>\n<p>Cookies are textual content or information stored by the server on the client side. They are mainly used to recognize a user on the web server.<\/p>\n<p>Cookies are sent from server to the client (browser). Client&#8217;s local machine contains this piece of information called Cookies. Further, all these cookies(in their domain) are sent to the server for all the subsequent requests made by the client so that the server can either identify a user or can use it for other purposes like tracking etc.<\/p>\n<p>Clients can flush cookies or they expire when they reach their maximum age. Some methods discussed below can define its domains.<\/p>\n<h3>Types of Cookies in JSP<\/h3>\n<p>There are basically two types of cookies:<br \/>\n<strong>1. Persistent cookies:<\/strong> These cookies are also known as permanent cookies. They remain on the hard drive and persist until the user deletes them or they expire themselves.<br \/>\n<strong>2. Session cookies:<\/strong> These cookies are also known as temporary cookies. They get deleted themselves as soon as the session ends or the browser closes.<\/p>\n<h3>Working of JSP Cookies(Uses)<\/h3>\n<ul>\n<li>A cookie can be used to remember the username and password for any site.<\/li>\n<li>A cookie can be used for Session management. The server may store a session id on the client side. When the method getSession() is called then this session ID can be sent by the client to the server to identify it.<\/li>\n<li>Session management using cookies fails for the users that keep their cookies blocked as no information can be saved on the client&#8217;s side.<\/li>\n<li>A cookie can be used to remember the preference of the user.<\/li>\n<li>We can use the method isRequestSessionIdFromCookie to see if the cookies are being used or not. If this method returns true then the cookies are being used otherwise not.<\/li>\n<\/ul>\n<h3>Structure of Cookies in JSP<\/h3>\n<p>A cookie sent by a JSP page in HTTP header looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">HTTP\/1.1 200 OK\r\nDate: Sat, 25 Apr 2020 21:03:38 GMT\r\nServer: Apache\/9.0.34 (WINDOWS) PHP\/4.0b3\r\nSet-Cookie: name = my_name; expires = Sun, 26-Apr-20 21:03:38 GMT;\r\n   path = \/\/; domain = identify.com\r\nConnection: close\r\nContent-Type: text\/html\r\n<\/pre>\n<p>A cookie saved in the browser may look like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">GET \/ HTTP\/1.0\r\nConnection: Keep-Alive\r\nUser-Agent: Mozilla\/4.6 (X11; I; Windows 10-15apmac ppc)\r\nHost: localhost.demon.co.in:1126\r\nAccept: image\/gif, *\/*\r\nAccept-Encoding: gzip\r\nAccept-Language: en_IN\r\nAccept-Charset: iso-8859-1,*,utf-8\r\nCookie: name = my_name\r\n<\/pre>\n<h3>Cookie Class<\/h3>\n<p>javax.servlet.http.Cookie class provides constructors and methods to extend the functionality of cookies in a JSP code.<\/p>\n<h3>Constructor for Cookies<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Constructor available<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Cookie()<\/span><\/td>\n<td><span style=\"font-weight: 400\">It is a constructor that will construct a cookie.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Cookie(String name, String value)<\/span><\/td>\n<td><span style=\"font-weight: 400\">It is a constructor that will construct a cookie with mentioned name and value.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>JSP Cookies Methods<\/h3>\n<p>JSP supports HTTP cookies. It does this using the servlet technology. Following are the methods used while working with the cookies in JSP.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>S.No.<\/b><\/td>\n<td><b>Method<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><b>public void setDomain(String pattern)<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method defines the domain of the cookies. For e.g. a particular cookie for a respective site.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><b>public String getDomain()<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method gets the domain of the cookie.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><b>public void setMaxAge(int expiry)<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will set the time for which cookie will remain. The expiry time is in seconds. If nothing is set, then it expires as the session ends.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><b>public int getMaxAge()<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will get the age of the cookie in seconds. If the age is not set, then it sends -1 that shows the cookie will expire when the browser closes.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">5<\/span><\/td>\n<td><b>public String getName()<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return or get the name of the cookie. A cookie\u2019s name can\u2019t change once it is set.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">6<\/span><\/td>\n<td><b>public void setValue(String newValue)<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will set the value of the cookie.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">7<\/span><\/td>\n<td><b>public String getValue()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will get the value of the cookie.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">8<\/span><\/td>\n<td><b>public void setPath(String uri)<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will set the path of the cookie i.e. the path it will get applied to. If undefined, the cookie gets applied to all the URLs of that directory.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<td><b>public String getPath()<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will get the path of the associated cookie.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<td><b>public void setSecure(boolean flag)<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method tells whether the cookie needs to be sent over encrypted connections or not.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">11<\/span><\/td>\n<td><b>public void setComment(String purpose)<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method gives a purposeful message to a cookie so that the user can understand the use of it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">12<\/span><\/td>\n<td><b>public String getComment()<\/b><\/p>\n<p><b>\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method returns the message of the cookie or null if it is not there,<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Cookies Handling in JSP through Set, read and Delete Cookies<\/h3>\n<p>Now that we have discussed the basics and methods of cookies along with a description, let\u2019s set, read, and delete cookies in JSP.<\/p>\n<h4>1. Setting cookies in JSP<\/h4>\n<p>Setting a cookie in JSP involves three important steps. These steps are<\/p>\n<p><strong>a. Creating an object of Cookie type<\/strong><\/p>\n<p>In this step we call a constructor i.e. Cookie. Create an object that will take the values. Pass two string values in this constructor that will be the key and cookie value. Syntax is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Cookie cookie = new Cookie(\u201ckey\u201d, \u201cvalue\u201d)<\/pre>\n<p>E.g. Cookie name = new Cookie(\u201cmy_name\u201d, request.getParameter(\u201cmy_name\u201d));<br \/>\nA cookie object and its value follows some naming conventions. They should not contain symbols like [ ] ( ) = , &#8221; \/ ? @ : ; and white spaces.<\/p>\n<p><strong>b. Set the age of cookie<\/strong><\/p>\n<p>Age of a cookie, as the name suggests, how long a cookie should survive. This can be set by setting the maximum age of the cookie. Age of a cookie is set in seconds. Syntax is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cookie.setMaxAge(seconds)\r\n<\/pre>\n<p>E.g. name.setMaxAge(60*60*24) (Max age of cookie is 24 hours in this example)<\/p>\n<p><strong>c. Pass it as a response in HTTP header<\/strong><\/p>\n<p>The third and final step to set a cookie is by adding it to the HTTP response headers. Syntax is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">response.addCookie(cookie);<\/pre>\n<p>E.g. response.addCookie(name);<\/p>\n<p>A detailed example with explanation has been shown below to set a cookie following all the above steps: The code below is for setting the cookies.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">one.jsp\r\n&lt;%\r\n   \/\/ Creating cookies for name and age.  \t\r\n   Cookie name = new Cookie(\"User_name\", request.getParameter(\"User_name\"));\r\n   Cookie age = new Cookie(\"Age\", request.getParameter(\"Age\"));\r\n   Cookie gender = new Cookie(\"Gender\", request.getParameter(\"Gender\"));\r\n  \r\n   \/\/ Setting expiry date\r\n   name.setMaxAge(60*60*24);\r\n   age.setMaxAge(60*60*24);\r\n   gender.setMaxAge(60*60*24);\r\n  \r\n   \/\/ Add both the cookies in the response header.\r\n   response.addCookie( name );\r\n   response.addCookie( age );\r\n   response.addCookie( gender );\r\n%&gt;\r\n \r\n&lt;html&gt;\r\n   &lt;head&gt;\r\n  \t&lt;title&gt;Set Cookies&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n  \r\n   &lt;body&gt;\r\n               \t&lt;h2&gt;--DataFlair--&lt;h2&gt;\r\n     \t&lt;h2&gt;Here we are setting Cookies&lt;\/h2&gt;\r\n  \t\r\n  \t&lt;ul&gt;\r\n         &lt;li&gt;&lt;p&gt;&lt;b&gt;User Name:&lt;\/b&gt;\r\n        \t&lt;%= request.getParameter(\"User_name\")%&gt;\r\n     \t&lt;\/p&gt;&lt;\/li&gt;\r\n         &lt;li&gt;&lt;p&gt;&lt;b&gt;Age:&lt;\/b&gt;\r\n        \t&lt;%= request.getParameter(\"Age\")%&gt;\r\n     \t&lt;\/p&gt;&lt;\/li&gt;\r\n               \t &lt;li&gt;&lt;p&gt;&lt;b&gt;Gender:&lt;\/b&gt;\r\n                         \t&lt;%=request.getParameter(\"Gender\")%&gt;\r\n               \t &lt;\/p&gt;&lt;\/li&gt;\r\n  \t&lt;\/ul&gt;\r\n  \r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>The above code will set the cookie: creating specified objects, setting their max ages and adding them to HTTP header response. Now we will make a simple form to get the values using the code below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n   &lt;body&gt;\r\n  \t\r\n  \t&lt;form action = \"one.jsp\" method = \"GET\"&gt;\r\n     \tUser Name: &lt;br\/&gt;&lt;input type = \"text\" name = \"User_name\"&gt;\r\n     \t&lt;br \/&gt;\r\n     \tAge: &lt;br\/&gt;&lt;input type = \"text\" name = \"Age\" \/&gt;\r\n     \t&lt;br\/&gt;\r\n               \t Gender:&lt;\/br&gt;&lt;input type = \"text\" name = \"Gender\"&gt;\r\n               \t &lt;br\/&gt;\r\n               \t &lt;input type = \"submit\" value = \"Go\" \/&gt;\r\n  \t&lt;\/form&gt;\r\n  \t\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Save the file in the ROOT directory of apache tomcat. First run the file hello.jsp. It will give the following output:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81432\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4.png\" alt=\"set cookies in jsp\" width=\"1366\" height=\"268\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4-300x59.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4-1024x201.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4-150x29.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4-768x151.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-4-520x102.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>Fill the details asked and click on the go button. Then you will get the following output as the one.jsp file runs.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81433\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4.png\" alt=\"setting jsp cookies\" width=\"1366\" height=\"292\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4-300x64.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4-1024x219.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4-150x32.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4-768x164.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-4-520x111.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>Thus we have set the cookies successfully.<\/p>\n<h4>2. Reading cookies in JSP<\/h4>\n<p>Reading the set cookies is a simple process. We need to create an object of Cookie type and an array of Cookie type. Then we will get all the cookies in this array using the request.getCookie() method of HttpServletRequest. We then apply if loop and get the name and value of these cookies using the method getName() and getValue()<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">two.jsp\r\n&lt;html&gt;\r\n   &lt;head&gt;\r\n  \t&lt;title&gt;Read Cookies&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n  \r\n   &lt;body&gt;\r\n  \t\r\n     \t&lt;h1&gt;Reading Cookies that we set&lt;\/h1&gt;\r\n \t\r\n  \t&lt;%\r\n     \tCookie my_cookie = null;\r\n     \tCookie[] my_cookies = null;\r\n     \t\r\n     \t\/\/ Get an array of Cookies associated with the this domain\r\n     \tmy_cookies = request.getCookies();\r\n     \t\r\n     \tif( my_cookies != null ) {\r\n        \tout.println(\"&lt;h2&gt; Found Cookies Name and Value&lt;\/h2&gt;\");\r\n        \t\r\n        \tfor (int i = 0; i &lt; my_cookies.length; i++) {\r\n           \tmy_cookie = my_cookies[i];\r\n           \tout.print(\"Name : \" + my_cookie.getName( ) + \",  \");\r\n           \tout.print(\"Value: \" + my_cookie.getValue( )+\" &lt;br\/&gt;\");\r\n        \t}\r\n     \t} else {\r\n        \tout.println(\"&lt;h2&gt;No cookies founds&lt;\/h2&gt;\");\r\n     \t}\r\n  \t%&gt;\r\n   &lt;\/body&gt;  \r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Running this code, we get the values of cookies we read. We get the following output.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81434\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3.png\" alt=\"reading set cookies in jsp\" width=\"1366\" height=\"360\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3-300x79.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3-1024x270.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3-150x40.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3-768x202.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-3-520x137.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>3. Deleting cookies in JSP<\/h4>\n<p>Now, while deleting the cookies in JSP, we follow similar steps to what we did in setting the cookies. We firstly read an already set cookie and then store it in an object of cookie type. Then we set the age of the cookie as zero in order to delete the cookie using the method setMaxAge(0) method. Then we add it to the Http Response Header so as to delete this cookie.<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">three.jsp\r\n&lt;html&gt;\r\n   &lt;head&gt;\r\n  \t&lt;title&gt;Delete Cookies&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n  \r\n   &lt;body&gt;\r\n  \t&lt;center&gt;\r\n     \t&lt;h1&gt;Delete Cookies&lt;\/h1&gt;\r\n  \t&lt;\/center&gt;\r\n  \t&lt;%\r\n     \tCookie my_cookie = null;\r\n     \tCookie[] my_cookies = null;\r\n     \t\r\n     \t\/\/ Get an array of Cookies associated with the this domain\r\n     \tmy_cookies = request.getCookies();\r\n     \t\r\n     \tif( my_cookies != null ) {\r\n        \tout.println(\"&lt;h3&gt; Found some Cookie Name and Value&lt;\/h3&gt;\");\r\n        \t\r\n    \t    for (int i = 0; i &lt; my_cookies.length; i++)\r\n                         \t{\r\n           \tmy_cookie = my_cookies[i];\r\n           \t\r\n           \t\/\/Deleting all the cookies\r\n              \tmy_cookie.setMaxAge(0);\r\n                  response.addCookie(my_cookie);\r\n       \t       out.print(\"Deleted cookie: \" +\r\n              \tmy_cookie.getName( ) + \"&lt;br\/&gt;\");\r\n           \t}\r\n           \tout.print(\"Name : \" + my_cookie.getName( ) + \",  \");\r\n           \tout.print(\"Value: \" + my_cookie.getValue( )+\" &lt;br\/&gt;\");\r\n       \t }\r\n      \telse {\r\n        \tout.println(\r\n        \t\"&lt;h2&gt;No cookies were founds&lt;\/h2&gt;\");\r\n     \t}\r\n  \t%&gt;\r\n   &lt;\/body&gt;\r\n  \r\n&lt;\/html&gt;\r\n<\/pre>\n<p>In this example, we deleted all the cookies that we set and read in the previous JSP programs. You can directly delete cookies in Internet explorer by clicking on the tools button, go to Internet settings and delete browsing history that involves cookies too. Below is the output of the code.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81435\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5.png\" alt=\"deleting cookies in jsp\" width=\"1366\" height=\"376\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5-300x83.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5-1024x282.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5-150x41.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5-768x211.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-5-520x143.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Example of JSP Cookies<\/h3>\n<p>Using the above code of hello.jsp, one.jsp and two.jsp we can view the values set for the cookies. In this example we have joined the setting and reading code(one.jsp and two.jsp) by adding a link of two.jsp in one.jsp. Below is the code and its output.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">one.jsp\r\n&lt;%\r\n   \/\/ Creating cookies for name and age.  \t\r\n   Cookie name = new Cookie(\"User_name\", request.getParameter(\"User_name\"));\r\n   Cookie age = new Cookie(\"Age\", request.getParameter(\"Age\"));\r\n   Cookie gender = new Cookie(\"Gender\", request.getParameter(\"Gender\"));\r\n  \r\n   \/\/ Setting expiry date\r\n   name.setMaxAge(60*60*24);\r\n   age.setMaxAge(60*60*24);\r\n   gender.setMaxAge(60*60*24);\r\n  \r\n   \/\/ Add both the cookies in the response header.\r\n   response.addCookie( name );\r\n   response.addCookie( age );\r\n   response.addCookie( gender );\r\n%&gt;\r\n&lt;html&gt;\r\n   &lt;head&gt;\r\n  \t&lt;title&gt;Set Cookies&lt;\/title&gt;\r\n   &lt;\/head&gt;\r\n   &lt;body&gt;\r\n  &lt;h2&gt;--DataFlair--&lt;h2&gt;\r\n     \t&lt;h2&gt;Here we are setting Cookies&lt;\/h2&gt;\r\n  \t\r\n  \t&lt;ul&gt;\r\n     \t&lt;li&gt;&lt;p&gt;&lt;b&gt;User Name:&lt;\/b&gt;\r\n        \t&lt;%= request.getParameter(\"User_name\")%&gt;\r\n     \t&lt;\/p&gt;&lt;\/li&gt;\r\n     \t&lt;li&gt;&lt;p&gt;&lt;b&gt;Age:&lt;\/b&gt;\r\n        \t&lt;%= request.getParameter(\"Age\")%&gt;\r\n     \t&lt;\/p&gt;&lt;\/li&gt;\r\n   &lt;li&gt;&lt;p&gt;&lt;b&gt;Gender:&lt;\/b&gt;\r\n   &lt;%=request.getParameter(\"Gender\")%&gt;\r\n   &lt;\/p&gt;&lt;\/li&gt;\r\n  \t&lt;\/ul&gt;\r\n   &lt;p&gt;&lt;a href=\"two.jsp\"&gt;Next Page will read the cookies&lt;\/a&gt;&lt;p&gt;\r\n   &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Output of above code is:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81436\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4.png\" alt=\"cookies handling in jsp\" width=\"1366\" height=\"344\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4-300x76.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4-1024x258.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4-150x38.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4-768x193.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-4-520x131.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Advantages of using JSP Cookies<\/h3>\n<ul>\n<li>Cookies are saved at the client\u2019s side by the server, thus reducing load at server side.<\/li>\n<li>It is the easiest and most efficient way to maintain the state.<\/li>\n<li>Cookies are the simplest way to identify a user.<\/li>\n<li>They can be useful for Session Management as well.<\/li>\n<\/ul>\n<h3>Disadvantages of using Cookies in JSP<\/h3>\n<ul>\n<li>Cookies contain only textual information which reduces its flexibility of use.<\/li>\n<li>If a client blocks the cookies then they will be of no use.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Finally we discussed cookies handling in JSP. We saw the basics of cookies, its working, structure, advantages, disadvantages etc. We discussed cookie class and methods available to support cookies in JSP etc. This article gave a detailed description about the setting, reading, deleting cookies in JSP.<\/p>\n<p><strong>Do share your feedback in the comment section if you like the article.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article discusses the cookies handling in JSP in detail. It discusses cookies, its types, working, structure. It also provides a description of setting the cookie, reading the cookies and deleting the cookies in&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":81431,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22403],"tags":[23095,23094],"class_list":["post-81357","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jsp","tag-cookies-handling-in-jsp","tag-jsp-cookies-handling"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JSP Cookies Handling with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"JSP Cookies Handling - Learn the basics of cookies, its working, structure, advantages, disadvantages. Learn about cookies types - persistent and session.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JSP Cookies Handling with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"JSP Cookies Handling - Learn the basics of cookies, its working, structure, advantages, disadvantages. Learn about cookies types - persistent and session.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-31T03:30:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:17:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JSP Cookies Handling with Examples - DataFlair","description":"JSP Cookies Handling - Learn the basics of cookies, its working, structure, advantages, disadvantages. Learn about cookies types - persistent and session.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/","og_locale":"en_US","og_type":"article","og_title":"JSP Cookies Handling with Examples - DataFlair","og_description":"JSP Cookies Handling - Learn the basics of cookies, its working, structure, advantages, disadvantages. Learn about cookies types - persistent and session.","og_url":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-08-31T03:30:34+00:00","article_modified_time":"2021-08-25T08:17:34+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"JSP Cookies Handling with Examples","datePublished":"2020-08-31T03:30:34+00:00","dateModified":"2021-08-25T08:17:34+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/"},"wordCount":1472,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg","keywords":["cookies handling in jsp","JSP Cookies Handling"],"articleSection":["JSP Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/","url":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/","name":"JSP Cookies Handling with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg","datePublished":"2020-08-31T03:30:34+00:00","dateModified":"2021-08-25T08:17:34+00:00","description":"JSP Cookies Handling - Learn the basics of cookies, its working, structure, advantages, disadvantages. Learn about cookies types - persistent and session.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/Cookies-Handling-in-JSP.jpg","width":1200,"height":628,"caption":"Cookies Handling in JSP"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/cookies-handling-in-jsp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"JSP Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/jsp\/"},{"@type":"ListItem","position":3,"name":"JSP Cookies Handling with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/81357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=81357"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/81357\/revisions"}],"predecessor-version":[{"id":81437,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/81357\/revisions\/81437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/81431"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=81357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=81357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=81357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}