Cookies in Servlets

Cookies are small piece of information used for session management.

The Http protocol is a stateless protocol, it means that each request is a new request for the Http server. Cookies is one of the ways in which Http server can persist data between multiple client requests.

Let me depict it with the help of an image:

When the browser sends a Http request for the first time, the servlet associates a cookie with the response and sends it to the browser. Now every subsequent request will be associated with the cookie and the server will use this information to identify the user and process the data accordingly.

There are two types of cookies:
1. Persistent Cookie -> It is available for a single session only. Once the user closes the browser the cookie is removed.
2. Non-persistent Cookie -> It is available for multiple sessions. Cookie is removed only when the user logs out.

Advantages of using Cookies:
1. The information is stored on the client side.
2. Simple

Disadvantages of using Cookies:
1. Only text data can be shared
2. Will not work, if cookies are disabled at client side.

Refer Your own Cookie! and Login Logout with Cookie

Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6