JSP Cookies Concept

Concept of Cookies :

  1. Cookies are basically text files stored on the client computer.
  2. Cookies are used for tracking purpose.
  3. JSP supports HTTP cookies using underlying technology using servlet.
  4. Cookies are created when you use your browser to visit a website that uses cookies to keep track of your movements within the site

Types of Cookies :

Type of cookie Explanation
Session cookies Session cookies are created temporarily in your browser's sub-folder when you visit the website for first time , once you leave the site these cookies will be destroyed automatically.
Persistent cookies Persistent cookies remain in the browser's sub-folder and activated when you re-visit the website. These cookies will be destroyed after the period set within the cookie's file

Steps to identify whether user is returning or not ?

There are three steps involved in identifying returning users:

  1. Server script sends a set of cookies to the browser when user visits site for first time. Cookies contain name,identification number etc
  2. Cookies will stored in the browser’s folder
  3. When user re-visit the website next time, browser sends those cookies information to the server and server uses that information to identify the user

cookies handling in jsp

Example of the Cookie File :

HTTP/1.1 200 OK
Date: Fri, 04 Feb 2014 11:01:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=pritesh; expires=Tuesday, 07-Oct-14 12:33:31 GMT; 
                 path=/; domain=c4learn.com
Connection: close
Content-Type: text/html

You can see the name-value pair in the cookie file.Cookie file contain GMT date, a path and a domain and expiration date.

Below is the example of the HTTP header that we send when user revisit the website.

GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz