Dealing with JSP Cookies


Cookies are text files stored on the client computer and they are kept for various information tracking purpose.

Cookies can be set , read and deleted by a programer by following below simple steps:

Setting Cookies 

           Setting cookies with JSP involves three steps:
  • Creating a Cookie object 
                     Cookie cookie = new Cookie("key","value");
  •  Setting the maximum age 
  •  Sending the Cookie into the HTTP response headers

Reading Cookies

    • To read cookies, you need to create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest. 

Deleting Cookies

  • deleting cookies is very simple. If you want to delete a cookie then you simply need to follow up following three steps:
  • Read an already existing cookie and store it in Cookie object.
  • Set cookie age as zero using setMaxAge() method to delete an existing cookie.
  • Add this cookie back into response

1 comment:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts.
    java training in chennai |
    java training institutes in chennai