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.


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

2 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

Naviya Nair said...
24 October 2016 at 15:27

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai

Post a Comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs