Back to Top

Friday, 17 February 2012

J2EE: SERVLET: Session Tracking in Servlets using Interface HttpSession


  • Session tracking in Servlets is done by using Interface HttpSession. It helps to identify a client throughout many page requests or visiting a website and to store details about that client.
  • Whenever a request arrives, it checks for the preexisting session object, to fetch details of the client from there, else, if its first request from a new client, its simple creates a session object for that client. These session objects are simple java objects.
  • HttpSession contains various methods to handle session object. HttpSession object lives in the server, and it automatically associated with the requester by internal mechanism like cookies or URL-rewriting
  • Whenever an application invokes or removes an object from a session, the session checks whether the object implemented the interface HttpSessionBindingListener or not. If yes, the servlet informs the object that it has been bounded to the session, if not, notifies unbound from the session. This alert goes after the binding methods complete.


Example:

HttpSession session = request.getSession(true);
/*  here, ‘request.getSession(true)’ - returns the current session associated with this request, or if the request does not have a session, creates one. */
     ClientDetails client_details = (ClientDetails)session. getAttribute("client_details");
if(client_details != null) {
//that means, existing client, can do something with the client_details
} else {
//create new client_details and store into the session
}



Anijit Sarkar

4 Responses to “ J2EE: SERVLET: Session Tracking in Servlets using Interface HttpSession ”

Java Experience said...
23 January 2013 at 22:57

readers would be interested to note that it is a bad practice to store data in the session of a user as session attributes as it increases load on the application and is vulnerable. for more details see session tracking in java ee


Unknown said...
16 April 2018 at 14:57

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Best Java Training Institute Chennai




pavankanna said...
8 October 2018 at 15:41

I respect your undertakings since it passes on the message of what you are endeavoring to state. It's an outstanding strength to make even the person who doesn't consider the subject could organized to grasp the subject. Your web diaries are sensible and likewise excessively portrayed. I should need to examine a dependably expanding number of fascinating articles from your blog. Keep Sharing
Tableau online training in India, Australia
Online Tableau training in Singapore, Malaysia


periyannan said...
25 April 2022 at 13:38

Thanks for one marvelous posting! I truly enjoyed reading it, you might be a great author. I will make sure to bookmark your blog and will come back in the future. I want to encourage that you continue your great job.

python internship | web development internship |internship for mechanical engineering students |mechanical engineering internships |java training in chennai |internship for 1st year engineering students |online internships for cse students |online internship for engineering students |internship for ece students|data science internships |


Post a Comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar