Thursday 19 September 2013

Implicit objects: session

Implicit objects: session



  • So when we talk about the data sharing in request we said that in previous example, since this value of a is only being used in jsp page only, I'm going to set this attribute a at the request scope.
  • So now lets say that I want that value of a to be printed multiple times in different jsp pages in my application, lets say that it is user name now to be printed . So when the user logs in to the system on each and every page I want to display the username at right hand top of the page.
  • Here the user makes request 1 and my application sends back a response back , and user makes another request, my application sends back another response so this can happen a number of times .
  • So you are constantly communicating with the user between different requests, we know that Http is a stateless protocol so Http really does not understand or does not really know one particular request is used from same user or different user. So Http protocol treats every new request is a completely different request and it is not able to differentiate between different request whether it is coming from same user or not? But when you are using "session" tracking in your application you will have ability to track your request, you will have ability to find whether the particular request is coming from a same client or different client.


Now lets talk about the servlet sessions
servlet sessions:



  • When you want to share a particular data or particular information across multiple pages for the same client so when the same client sends multiple requests its nothing but a session so 'n' number of requests and responses from the same client is nothing but a one session. 
  • And you want some kind of data to be persistant across multiple requests for the same client you save that value in the session object, in this case I save the value in session object, so Im going to save user name at the session level.
  • Now Im going to forward that request to jsp, where again im retrieving it using the  session implicit object and display on that page.

No comments:

Post a Comment