Session Tracking Methods

Last modified on September 7th, 2014 by Joe.

Following answer is applicable irrespective of the language and platform used. Before we enter into session tracking, following things should be understood.

What is a session?

A session is a conversation between the server and a client. A conversation consists series of continuous request and response.

Why should a session be maintained?

When there is a series of continuous request and response from a same client to a server, the server cannot identify from which client it is getting requests. Because HTTP is a stateless protocol.

When there is a need to maintain the conversational state, session tracking is needed. For example, in a shopping cart application a client keeps on adding items into his cart using multiple requests. When every request is made, the server should identify in which client’s cart the item is to be added. So in this scenario, there is a certain need for session tracking.

Solution is, when a client makes a request it should introduce itself by providing unique identifier every time. There are five different methods to achieve this.

Session tracking methods:

  1. User authorization
  2. Hidden fields
  3. URL rewriting
  4. Cookies
  5. Session tracking API

The first four methods are traditionally used for session tracking in all the server-side technologies. The session tracking API method is provided by the underlying technology (java servlet or PHP or likewise). Session tracking API is built on top of the first four methods.

1. User Authorization

Users can be authorized to use the web application in different ways. Basic concept is that the user will provide username and password to login to the application. Based on that the user can be identified and the session can be maintained.

2. Hidden Fields

<INPUT TYPE=”hidden” NAME=”technology” VALUE=”servlet”>
Hidden fields like the above can be inserted in the webpages and information can be sent to the server for session tracking. These fields are not visible directly to the user, but can be viewed using view source option from the browsers. This type doesn’t need any special configuration from the browser of server and by default available to use for session tracking. This cannot be used for session tracking when the conversation included static resources lik html pages.

3. URL Rewriting

Original URL: http://server:port/servlet/ServletName
Rewritten URL: http://server:port/servlet/ServletName?sessionid=7456
When a request is made, additional parameter is appended with the url. In general added additional parameter will be sessionid or sometimes the userid. It will suffice to track the session. This type of session tracking doesn’t need any special support from the browser. Disadvantage is, implementing this type of session tracking is tedious. We need to keep track of the parameter as a chain link until the conversation completes and also should make sure that, the parameter doesn’t clash with other application parameters.

4. Cookies

Cookies are the mostly used technology for session tracking. Cookie is a key value pair of information, sent by the server to the browser. This should be saved by the browser in its space in the client computer. Whenever the browser sends a request to that server it sends the cookie along with it. Then the server can identify the client using the cookie.
In java, following is the source code snippet to create a cookie:

Cookie cookie = new Cookie(“userID”, “7456”);
res.addCookie(cookie);

Session tracking is easy to implement and maintain using the cookies. Disadvantage is that, the users can opt to disable cookies using their browser preferences. In such case, the browser will not save the cookie at client computer and session tracking fails.

5. Session tracking API

Session tracking API is built on top of the first four methods. This is inorder to help the developer to minimize the overhead of session tracking. This type of session tracking is provided by the underlying technology. Lets take the java servlet example. Then, the servlet container manages the session tracking task and the user need not do it explicitly using the java servlets. This is the best of all methods, because all the management and errors related to session tracking will be taken care of by the container itself.

Every client of the server will be mapped with a javax.servlet.http.HttpSession object. Java servlets can use the session object to store and retrieve java objects across the session. Session tracking is at the best when it is implemented using session tracking api.

Comments on "Session Tracking Methods"

  1. […] after all, software is all about mimicking real life.  In a previous article I discussed about methods used for session tracking. It has fundamental information about what a session is and how to manage it. At the end of that […]

  2. anil says:

    nice article i love it

  3. Paras says:

    Pls read “conversation” in place of “conversion” above in the paragraph “What is a session?”.

    Hey, Joe, Please correct me if I am wrong.

  4. Joe says:

    Thanks Paras for pointing the spelling mistake. I have rectified that.

  5. vinoth says:

    Joe. I like this article that u have
    presented.My humble request, Try to
    add “Struts”…..

    -p.vinothkumar

  6. nirmala says:

    nice one i like it

  7. ananya says:

    spelling of alongwith not alogwith in topic cookies

  8. chandu says:

    its better to refer ur experiences before going to attend interviews. its a fabulous website which includes not only matter but also the terrific doubts which are posted by the fans, thank u

  9. ajit says:

    i like your formate . it is very good and simple to understood.

  10. Joe says:

    @ananya – thanks, corrected.

    @Vinoth – I will try to include soon. :-)

  11. ramesh says:

    oh…very nice. simple understandble language..very nice article
    thanks …

  12. guru says:

    nice…. commmands.. it easy to apply the progrms..

  13. Dhaval says:

    very nice explanation……thanx.also,i liked the design of your web page.

  14. mohan says:

    Very good article… simple understandble language … nice one…… simple .. good.

  15. Prasad Khamkar says:

    Nice

  16. sandeep sharma says:

    Sir please add the coding how add & get session in servlet

  17. John Ortiz says:

    Excellent article: clear and understandable.

  18. rekha says:

    simple and understandable sir..plz give the code to add cookie

  19. Amit says:

    Thnaks..NIce job

  20. Madhur says:

    Nice, easy language…

  21. samina says:

    A.O.A!
    really a helpful material, it helped me great in understanding concept.
    thnx!

  22. vivek says:

    nice article….can you post more core java articles it may be useful for beginers

  23. Renuga says:

    Its really good…..
    I studied for exam through this blog only…….
    Lot of thanks………

  24. babu says:

    soooo good help full to me

    thanks a lot

  25. Sophia says:

    That was extremely useful and very simple explained. Thanks

  26. prakash says:

    thanks for the tutorial on session in java.
    explained simply.

  27. archana says:

    Iyt help me a lot to understand briefly about session tracking

  28. Sindhu K says:

    Brief n precised explanation about session tracking… Gud one!!!

  29. sharavathi says:

    WOW excellent article.
    very useful for beginner.
    Thank u very much for sharing your experience

  30. Krishnakant Kadam says:

    It is really nice format and easy to uderstand …and help me brief Session tracking API using servlet ….
    i am waiting for reply

  31. Anonymous says:

    very nice… thank you for explaining in detail

  32. bharath says:

    Thanks for clear delivering of Good info. about the Session Tracking

  33. Ashish Mahana says:

    Its really very clear to understand,

    In the cookie section u should also mention about the server side cookie mantainance(i.e. how server know that this cookie is meant for a particular user?).

    I like your article.

  34. Anonymous says:

    it’s very easy to understand,

    try to prefer this one, may be it give an help to you…

    KRS RAJASEKHAR ON January 18th..

  35. Leela says:

    Good explanation

  36. neha says:

    good explanation but i need examples also

  37. Amit says:

    Joe, your presentation skills are quite good.. thanks for this stuff..

  38. vissu says:

    Good One But It will be helpful if mention the Disadvantage of First Method

  39. chinna says:

    good explanation

  40. veera says:

    nice explanation for beginners of java

  41. Anjanidevi says:

    VeRy Good Explaination..It will be the best if you can give a simple example for ech type…

    Anjani

  42. Anonymous says:

    Really its very easy to understand..

  43. Anonymous says:

    Really its very easy to understand..

  44. Anonymous says:

    thank u very much…………….

  45. Surekha says:

    Nice Article..Very good to understand and pratice for freshers who does not have any knowldge on sessionn

  46. Anil says:

    helloo sir..U r article abt SESSION TRACKING was very informative…am doing my BTECH Final yr proj on session tracking…i need ur guidence for that.

    plz help me sir..
    can you give me ur contact number or ur email-id.

  47. janmejaya sahoo says:

    hayeeee…. its a good article……
    helpful…

  48. monika says:

    Superb Blog!!

  49. harry says:

    Dear Mr.Joe,
    Can you please elaborate on the 5th way – session API. i think it needs little more explanation.

    Anyways nice blog, very informative n of all, very simple

    Thanku so much!!!

  50. Anonymous says:

    all your blogs are really clear and understandable…
    thank you so much for sharing!!

  51. pallav rajput says:

    very much clear!!! nice work…….

  52. AbdulQadir says:

    its realy very good, and easy to understand
    tanku so much

  53. sri says:

    its really good,it helped me a lot thankq

  54. Julias Ceasor says:

    Useful content.. It is very precise and clear…
    Thanks

  55. Karunakar says:

    its really simple and awesome…….. thanks

    but sir, there are only 3 questions covered on Collections, you know it sir Java Collections is the heart of Java………. i kindly request you post some more on Collections and the difference between them.

    Thank you, once again your site is a bible for Java guys…

  56. GuravaReddy says:

    will you maintain small example concept wise it is very good.
    ok Anyway It’s a nice nice blog.

  57. Anonymous says:

    it’s really good..simply to understood…
    thank you very much

  58. Ganesh Shinde says:

    Its really good one.. I like your articles.

  59. sneha says:

    really nice!!
    very easy and simple language to understand!!
    Thanks a lot!!

  60. Ram Kumar says:

    simple and understandable.

  61. Chintan says:

    Hi JOE,
    On index of this blog i think this post must be in JSP or SERVLET section.

    I like your blogs, that’s why i suggest this change.

    Point me if i am wrong.

    Thanks.

  62. purva says:

    hi joe,

    its really nice….

  63. purva says:

    hi joe,

    its really nice….

  64. Anonymous says:

    It’s a nice article and very easy to understand. You presented it using simple language.

  65. Anonymous says:

    if example is presented simply superb

  66. soma says:

    thanks ,its very to under stand for students

  67. Anonymous says:

    Nice web design

  68. Anonymous says:

    Nice web design

  69. muralidhar says:

    it is very nice……
    thanks a lot

  70. Anonymous says:

    thank’s sir !!!!!!!!

  71. Anonymous says:

    thanks sir

    its easy to understand

  72. ramani.m says:

    thanks sir

    its easy to understand

  73. Anonymous says:

    thnx Joe
    you are doing good for itians.
    it really to understand concepts from beginning.

  74. Ragav says:

    thanks a lot sir, can u give more concepts of java like this it will be useful for us

  75. Ragavendira B says:

    Thank u very much sir,It is very simple and easy for understanding. can You Give me other java concepts also like this it will more useful for my career sir.

  76. Deepak says:

    Ultimate Article

  77. Brijesh Patel says:

    Evey time I get conceptual understanding with vast meaning.

  78. K.E.Naresh says:

    sir,in the above 5 methods which is the best one to implement in realtime

  79. Netaji says:

    thanx for a wonderful article…!

  80. murali says:

    hi Joy,

    Nice preparation. One suggestion from my side. Could you explain briefly about session management using Session API. It could be great.

    -cheers,
    Murali.

  81. Tony says:

    Hi Joy

    one suggestion from my side can u include both Hibernate and Spring.

  82. Paritosh says:

    Very nice and useful article,Joe

  83. Anonymous says:

    sir, This is a spectacular article. i loved it… awesome

  84. Ishleen says:

    All your articles are simply awesomeeee..:-) Easy to understand..

    Thanks a lot..

  85. Ashok says:

    Excellent

  86. ram says:

    useless……not sufficient

  87. arulprasath says:

    Brevity is the soul of wit! And this article is of that kind! Very simple,very clear and very very effective!

  88. prashant says:

    @ajit: dude.. improve ya english!!

  89. prashant says:

    u r awesome joe!!

  90. Shruti says:

    I am in awe of your lucid explanation for every single topic (i have come across!).

  91. Anonymous says:

    ya its very useful to the beginers……thanks
    for your information …it helps me a lot…!
    kamalakar reddy

  92. JAP says:

    I like the simplicity in the explanation!!!

  93. Anonymous says:

    thanks, its very easy language explain,everything know about session tracking
    once again thanks very much

  94. pavan kumar says:

    thanks, its very easy language
    explain,everything know about session tracking

    thanks thanks thanks

  95. kanchana says:

    Plz explain wit examples..specially URL Rewriting..

  96. Ashwini says:

    Whats the purpose of session tracking?

  97. karna.poreddy says:

    HTTP protocol is state less protocol in order to maintain the state we will use session tracking mechanism
    for example :
    i need to buy a book in online shopping
    and a pen first i bayed a book the HTTP request holds the book now again i wanna buy a pen now the previous request will be replaced by new request that is pen in order to maintain two requests we use session tracking mechanism
    Mainly we have 4 types of session tracking Mechanism:Http Session
    URL rewriting
    hidden form fields
    cookies

  98. venkat says:

    in the hidden field method of sessin tracking

    This cannot be used for session tracking when the conversation included static resources like html pages.

    what is this mean? could you please explain this?

  99. vilkas says:

    It’s really super

  100. RangaReddy Julakanti says:

    plz Give me details about Session scope.Existing session information.if we add any values to session object.then those are not thread safe.how to overcome this problem.sir please give information with practicallly what u r saying.

  101. santhosh says:

    I am new to java, Please guide me programing as well as coding with more examples.

    Thanks in Advance.

  102. Anonymous says:

    what if a browser doesn’t accept cookies i.e if cookies are disabled

  103. Ramya says:

    Superb….

  104. amit says:

    good one this is very understable manner artical very useful and effective

  105. Manjusha says:

    Very nice article. Thank you Joe..

  106. pratik bhattad says:

    its nice artical…..easy way

  107. Anil says:

    Need more programmatic explaination. Rest good as usual. Thanks

  108. vijaya says:

    gud one

  109. Suresh says:

    i want example on session Tracking methods

  110. Pradyumna Swain says:

    Superb…simple explanation..i love it…

  111. Anonymous says:

    good explanation

  112. sriram prasad says:

    Easy to understand but need some more deep explanation

  113. Anonymous says:

    very simply written article…….

  114. Anonymous says:

    i like this topic .it is easy to understand……thanks

  115. Raki says:

    Please kindly let me know how to track the session by using SessionTracking API

  116. Aditya says:

    Nice article.. till now I didnt have clear idea on the session Mngt. Now I have it.
    Thanks for taking time to write such a good articles.

    Aditya

  117. Chinmay says:

    This session tracking is only possible for a single server. How would you track a user session when there are clusters available and every request doesn’t land in the same server all the time?

  118. Moiz Lokhandwala says:

    Sir, I want to get the session of one servlet on another servlet. For ex:-
    If I log in a user and a page is displayed then I click on account info,I should get the username again..

    How can I do that….

    Please help me out…

  119. @Murthy says:

    nice article

  120. sravan says:

    I like very much please provide programs for allthese things

  121. avinash says:

    can u explain with example using session tracking API

  122. Anonymous says:

    EXcellent!

  123. Anonymous says:

    hey joy please add the explanation of web.xml used in servlet.

  124. Anonymous says:

    ananya content is important and not spelling

    -amit

  125. nandu says:

    very nice article …thanks

  126. Sekhar says:

    Its Very Good Article.Helped But Still need Some more . Shall I get session Object again with the help of any of the following session.getId(); , creationdate() or anything else.
    I am depending on other site for some processes in my project.There I am lossing my session values when Url changes . How to retrack the session values back .

  127. Anderson says:

    hey,
    That is a nice tutorial.Explained in depth. Me too want your articles on struts spring and hibernate as these frameworks are a lot in demand now-a-days.Thank you.

Comments are closed for "Session Tracking Methods".