Note: You can see the JESSIONID (Session ID) in the output screens at the end.
Note: For better understanding, the subject is discussed in Question/Answer format.
Note: Before going into this example, it is advised to go through the Cookie API methods and their explanation because these methods are used in this example.
1. What is Cookie?
For us, Cookie is a class from javax.servlet.http package. But in the open world, a Cookie represents a sweet Biscuit or Chocolate.
2. What a Cookie can store?
Cookie stores small strings of data. Cookies stay all through the session. Programmer can store the session data with Cookie object. It is another style of tracking session data (the first way is using HttpSession interface).
3. Who creates Cookies and where they are stored?
Servlet Cookies are created on the server (by Servlet container) and sent to the client's browser for temporary storage. The Servlet Cookies can be sent to and fro between client and server in their conversation. Client is not aware of the entire process of Cookie management as everything is done by the server and client is kept completely transparent.
4. What Cookie can do in Servlets?
Servlet Cookies are used mainly for tracking different types of client's information. That is, the Web server can store data on the client browser in the form of Cookie objects.
Typical uses of a Cookie are
5. How Servlet Cookies stored on the client are recognized by the server later? The Cookie object created on the server is attached to response object and sent to client. The Cookie carries implicitly (as response header) the server information like name, path, host and connection type etc. and the combination of this information is used by server to know cookie is coming from which client.
6. All Cookies are same or any types exist?
Yes, there are two types of cookies and they are differentiated on their life span.
With this knowledge of Cookies, let us go for an example. In this code, we create a cookie in the Servlet, sent to client to store on the browser, later retrieved from client and information available with the cookie is printed.
The entire program moves around a Shopping cart, an important module of e-Commerce application. While going to a Shopping Mall, nearby to our house, physically we take a wheel cart to place the items we would like to buy. But as our program represents an online Mall, here shopping cart is imaginary but have all the operations you do on a physical cart like adding items, deleting, replacing, searching etc. For simplicity, only I took two operations only of Adding items and Searching (or Listing) items and these operations are performed through Servlet Cookies.
As usual there exist two programs of Client HTML and server Servlet.
HTML File: ShoppingCart.html
<h3>Cookie Example through Shopping Cart</h3> <form method="get" action="http://localhost:8888/india/SC"> Enter Item Name <input type="text" name="item"><br> Enter Item Quantity <input type="text" name="qty"><br> <input type="submit" value="Add Cookie" name="add"> <input type="submit" value="List Cookies" name="list"> </form>
Observe, fist time we are having two submit buttons in a single