How Container Does The Cookie Work in J2EE Define Cookie Cookies Explained Session In Servlet What Is Cookie How Apache Tomcat Handles Cookies Cookies Explained How Cookies Work Cookie Guide

How container does the cookie work?


A Cookie is a key value pair primarily used by the container to pass the session id in order to maintain session between client and server. There can be user defined cookies as well to use cookie as attributes to pass values from one part of the web application to another.

The user only requests for the session,creating a session object,attaching the session id with the session object and sending it to the client side or the browser with the response is done by the server at the back end. These things the code has nothing to bother about,the code asks for the session and these things happen automatically.


The code below shows how to get a session object in servlet :

HttpSession sess = request.getSession();


With the help of the above written code,the session can be retrieved from the server.Following are the steps covered by the server as soon as this line of code executes :

1. HttpSession object is created

2. Unique Session id is created

3. A new cookie object is created.

4. Session id is associated with cookie

5. Cookie is set Into the response to be sent to client side


These are the steps involved in sending the cookie with the response if session does not exist .

The same code when executed does the work of retrieving a session object from request if it exists.If session already exists,the server finds the session id from the request(since cookie will be sent to the server from client side as part of the url).The server finds out the unique session id from the cookie and retrieves the session object with the matching Session id.

Thus all the cookie work happens behind the scene.

To know whether the session already existed or was just created,we can ask the session object itself as :


If it returns true ,it means session is created otherwise it is a preexisting session.

Number of View :5053

Tags: , , , , , ,

  1. No comments yet.

Content Protected Using Blog Protector By: PcDrome.