Saturday 1 May 2010

What is HttpSessionListener

·         We knew that Http protocol is a “stateless” protocol means it can not remember the previous transactions.
·         When client sends a request to the servler, the server receives the request and processes the request and sends back the response to the client.
·         After sending the response the server to the client, the server closes the connection and forgets about the previous requests.
·         If the client sends the request to the server again, the server treats each request as a new request.
·         So to remember the server of each transaction of client request we use the session.
·         In session tracking when the client sends the request to the server then server creates a unique id for that request and sends back the unique id to the client along with the response object, and when a client sends the request to the server it also sends a unique id with it so that the server remember from where the request is coming.
What is Listeners:
Listeners listen the events. Whenever any event occurs it is listened by the listener object. The listener object is controlled by the web servers. Listeners allow developers to recognize the lifecycle events of a web application so that they can perform common tasks. For example: 

  •  A developer can execute the code for initializing a database when an instance of a Web application is created. 
  • Here, creating an instance( object) of a web application is an event. The listener Application Programming Interface (API) has predefined methods to listen to such events.
HttpSessionListener is an interface which extends java.util.EventListener class. The main purpose of this listener is to notify whenever there is a change in the list of active sessions in a web application 
This interface has  two methods:
  1. sessionCreated(HttpSessionEvent event): It will notify when the session is created.
  2. sessionDestroyed(HttpSessionEvent event): It will notify when the session gets invalidated.
In the above methods we can see that we have used HttpSessionEvent as the parameter of both the methods. This class has only one method getSession() which returns the current session.

No comments:

Post a Comment