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 :
sess.isNew();
If it returns true ,it means session is created otherwise it is a preexisting session.
Number of View :2362Related posts:
- Concept Of Session In J2EE Session Tracking In J2EE Session Management Seesion Concept Servlet Basics Servlet Guide Session Tracking
- Basics of tomcat Apache-Tomcat Tutorial Download and Run Tomcat
- A Hello,World Servlet Servlet Tutorial Servlet Examples Run servlet on tomcat
- Tomcat Welcome file list Tomcat directory listing setup welcome files in tomcat
- Tomcat Directory Tree structure Tomcat Fundamentals Tomcat Administration
Tags: apache tomcat server, cookie, j2ee, java, servlet, session, Session tracking
This entry was posted on Wednesday, May 9th, 2012 at 2:59 am and is filed under Java, OPPS, Tech-Tips, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.