Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Servlet Cookies

Shirin Husain Patel.


T18504
What is A cookie?
 A cookie is a small piece of data sent from a website and stored in the
user’s web browser while the user is browsing it.
 Every time the user loads the website, the browser sends the cookie back
to the server to notify user’s previous activity.
 Cookies are designed to be a reliable mechanism for websites to remember
state full information or to record the user’s browsing activity.
 Cookies are limited in size but the possibilities of data storage in them are
endless.
Who is the inventor of cookie?

 The term cookie is derived from the word “magic cookie” and is invented
by the web browser programmer “LOU MONTULLI”
 We can call a cookie as a web cookie, internet cookie, browser cookie, or
HTTP cookie.
 A definitive specification for cookies are used in the real world was
published as RF 6265 in April 2011.
Why are cookies used?
 Cookies are a convenient way to carry information from one session on a website
to another, or between sessions on related websites , without having to burden a
server machine with massive amounts of data storage.
 Storing the data on the server without using cookies would also be problematic
because it would be difficult to retrieve a particular user’s information without
requiring a login on each visit to the website.
 If there is a large amount of information to store , the a cookie can simply be used
as a mean to identify a given user so that further related information can be
looked up on a server-side database.
How does a cookie work?
 Whenever we are visiting a website for the 1 st time it stores our
personal preferences in our web browser on a cookie.
 On the 2nd on the second time when we visit the same website there is
no need to provide the details again as the website can remember our
preferences through the cookies.
 The web browser, on the 2nd visiting sends the stored cookies back to
the only related server to notify user’s previous activity.
Who can see the How long does a cookie
cookies? last?

• Only the server who put the • The time of expiry of a cookie
cookie on your hard drive. can be set when the cookie is
created.
• Larger sites can set a domain
attribute for other servers in the • By default the cookie is
same sub domain. destroyed when the current
browser window is closed, but it
can be made to persist for an
arbitrary length of time after that.
Are cookies harmful?
 There is a lot of concern about privacy and security on the internet.
Cookies do not in themselves present a threat to privacy, since they can
only be used to store information that the user has volunteered or that the
web server already has. Whilst it is possible that this information could be
made available to specific third party websites, this is no worse than
storing it in a central database. If you are concerned that the information
you provide to a webserver will not be treated as confidential then you
should question whether you actually need to provide that information at
all.
Types of cookies.
1) Session cookie/ Transient cookie/ In-memory cookie.
2) Persistent cookie/ Permanent cookie.
3) Secure HTTP cookie.
4) HTTP only cookie.
5) Third party cookie.
6) Super/ Zombie cookie.
 In modern web two most important cookies are:
1. Authentication cookie
2. Tracking cookie.
Session cookie.
 A session cookie contains information that is stored in a temporary
memory location and then subsequently deleted after the session is
completed or the web browser is closed.
 This cookie stores information that the user has inputted and tracks the
movements of the user within the website.
 It is only valid for one session.
 As the browser is stored in memory, this cookie is also stored in memory.
As the browser goes out of memory the cookie is also deleted.
Persistent Cookies.
 Persistent cookies are stored in hard drive.
 As they are stored in hard drive, they are stored for more then one
browser session.
 Persistent cookies stay with your browser even after you have exited it.
 The next time you open your browser and access the website that
placed the cookie on your browser, it will find the cookie still there.
 They are actually very helpful.
Where are cookies used?
 To create a temporary session where the site in some way remembers in the short
terms. For example:
a) Who the user is logged in as at the moment.
b) What the user has ordered from an online shopping cart.

 To remember low-security information more permanently. For example:


a) A user's search results preferences
b) What topic had the user browsed on the user's last visit
• To compile user statistics, for example for advertising purposes or for improving the
functionality of a site.

• To identify a user during an E-commerce session.

• To avoid keying in username and password to login the site.

• Cookies are used in different web applications. Some of them are listed below:

a) Shopping Cart Applications.

b) Online Banking.

c) Generation of a visitor's profile.

d) Fee based services.

e) Website Tracking.
Advantages and Disadvantages of cookies.

 Advantages:
a) Cookies can easily store more data than hidden fields.
b) Data is stored on the client computer, not the server which saves space on the server and
will not have any effect on server performance.
 Disadvantages:
a) Data is stored on the client computer, which means the data is neither safe nor secure.
b) Cookies are good for keeping session data not user data .
c) Cookies may be discarded or the user may contact the server from another computer.
d) Users can tell browsers to turn cookies off.
How to create a cookie?
 //creating cookie object
Cookie ck=new Cookie("user","shirinhusain");

 //adding cookie in the response


response.addCookie(ck);
How to delete a cookie?
 //deleting value of cookie
Cookie ck=new Cookie("user",“ ");

 //changing the maximum age to 0 seconds


ck.setMaxAge(0);

 //adding cookie in the response


response.addCookie(ck);
How to get cookies?
 Cookie ck[]=request.getCookies();
//printing name and value of cookie
for(int i=0;i<ck.length;i++)
{
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
}
THANK – YOU

You might also like