Dealing with JSP Cookies

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 
                     cookie.setMaxAge(60*60*24);
  •  Sending the Cookie into the HTTP response headers
                      response.addCookie(cookie); 

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

Newer Post Older Post

One Response to “Dealing with JSP Cookies”

  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

    ReplyDelete

naeemgik. Powered by Blogger.