Session Tracking Methods

31/05/2008

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.

Ads by Google

154 comments on “Session Tracking Methods

  1. Pingback: Session Life Cycle

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

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

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

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

    -p.vinothkumar

  5. spelling of alongwith not alogwith in topic cookies

  6. 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

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

  8. @ananya – thanks, corrected.

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

  9. Ads by Google
  10. oh…very nice. simple understandble language..very nice article
    thanks …

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

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

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

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

  15. this is very good artical to explain about sessiontracking .how we implemeted sessiontracking api in our application how it will create session id for every client

  16. Really Useful Stuffs, Thanks for such a blog

  17. Nice article…
    It’s very very clear..

    Thank u boss…

  18. Simply superb !!!! Thanks for using the simple language . . .

    • 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.

  19. after readin this i dont hav any doubt..thx for such blog

  20. Anything simple always interests me.
    thank you Joseph

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

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

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

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

  25. That was extremely useful and very simple explained. Thanks

  26. Iyt help me a lot to understand briefly about session tracking

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

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

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

  30. very nice… thank you for explaining in detail

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

  32. 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.

  33. it’s very easy to understand,

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

    KRS RAJASEKHAR ON January 18th..

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

  35. session_id()
    must be placed before v the o/p is excuted
    so the client user id vil be displayed at the output @ashish mahana

  36. list out various methods how a session vil start

  37. Nice article…Very easy to capture…

  38. Really an understandable one…. Thank uuuuu

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

  40. very nice article plz add struts also
    thanks
    sreenjvas

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

    Anjani

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

  43. 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.

  44. hayeeee…. its a good article……
    helpful…

  45. 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!!!

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

  47. very much clear!!! nice work…….

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

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

  50. 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…

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

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

  53. Its really good one.. I like your articles.

  54. nice article but i m bit confused with url Rewriting…:(

  55. Fantastic… Gives More Interest to learn…

  56. THanks a lot!!!!!!!
    Helped Me a lot!!!!!

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

  58. 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.

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

  60. if example is presented simply superb

  61. thanks ,its very to under stand for students

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

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

  64. 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.

  65. very easy to under stand & it is simple

  66. Evey time I get conceptual understanding with vast meaning.

  67. excellent ….very gud explaination os session tracking…thanks a lot

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

  69. very clear and understable…thankyou

  70. Its a Fantastic Article..!!!
    V.Prabhakaran

  71. 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.

  72. Hi Joy

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

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

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

    Thanks a lot..

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

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

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

  78. I like the simplicity in the explanation!!!

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

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

    thanks thanks thanks

  81. Plz explain wit examples..specially URL Rewriting..

  82. hi i am sourabh
    its really good

  83. 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
    if u hve any doubts mail me on karna.poreddy@gmail.com

  84. commendable work done by u … thanks a lot

  85. 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?

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

    Thanks in Advance.

  87. IT IS VERY SIMPLE AND EASY TO UNDERSTAND YOUR FORMAT……..
    THANK YOU FOR PROVIDING SUCH A WONDERFUL INFORMATION…

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

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

  90. Need more programmatic explaination. Rest good as usual. Thanks

  91. i want example on session Tracking methods

  92. Easy to understand but need some more deep explanation

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

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

ABOUT
I am Joe, author of this blog. I run javapapers with loads of passion. If you are into java, you may find lot of interesting things around.
Ads by Google
STAY in TOUCH:

Email:

Core Java | Servlet | JSP | Design Patterns | Android | Spring | Web Service | © 2008-2012 javapapers.