Introduction to Session Life Cycle, SSL Sessions, Passivation and Session Related Listeners

We have already seen basic session management in Java EE using cookies and URL rewriting. Now we will quickly see some of the remaining important topics such as SSL sessions, session life cycle, passivation and sessions in distributed applications.


Session Life Cycle

There are three stages in the life of a session

  1. New
    1. Until session is actually being established with the client.
  2. Joins a session
    1. Session will be always new if client does not join a session
  3. Destroyed
    1. session is invalidated or session timeout period expires


It is impossible for server to know if client has left the session or not and hence a session can never be terminated. This is because, unlike stateful protocols like FTP, HTTP is stateless. However server will maintain a session timeout and if there are no requests from the client for that entire timeout duration, the session will be invalidated.

A session timeout is defined through the deployment descriptor using session-timeout sub-element of session-config and the timeout is specified in minutes. If 0 or negative, sessions will never timeout. If element is omitted, java ee server can use its own values and average is usually around 30 minutes. There is no annotation alternative for this.

If client needs to request for a session to be invalidated, then server needs to provide an explicit method or means to do that like a logout link. Unlike stateful protocols like FTP, stateless protocols like HTTP do not have a close method. In case of a stateful protocol, if the client step away from communication, the connection is automatically and there is no need for an explicit request as in the case of stateless protocols.


SSL Sessions

TLS/SSL provides inherent connection tracking mechanism in its connection between the client and server. The container can use this mechanism to track the client when the client is using an SSL based protocol such as HTTPS (Secure HTTP). Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer (SSL), are cryptographic protocols designed to provide communications security over a computer network. They use X.509 certificates and hence asymmetric cryptography to authenticate the counterparty with whom they are communicating, and to exchange a symmetric key. This session key is then used to encrypt data flowing between the parties. In OSI model equivalences, TLS/SSL is initialized at layer 5 (session layer) and works at layer 6 (the presentation layer). The session layer has a handshake using an asymmetric cipher in order to establish cipher settings and a shared key for that session; then the presentation layer encrypts the rest of the communication using a symmetric cipher and that session key. We will see TSL/SSL and their session mechanism later in security notes.



Passivation is the process of serializing a session. This may be done either to transport sessions over a network (Session Migration) or to save inactive objects with longer objects to persistent storage (HD) to save memory (RAM). Passivation is similar to operating system’s virtual memory where older RAM memory may be written to persistent storage (HD) to save memory (RAM).

Container can only interact with active object forms of a session. So when a request is made in an already passivated session, then the session needs to be read back into memory from its serialized form and activated again. There is a listener interface HttpSesssionActivationListener which can listen to the passivate and activate events.

Session migration is the transfer of session objects across web containers via serialization to handle requests in distributed applications where the request can come to any of the available servers. Therefore if sessions need to be migrated (or passivated), all objects added to an HttpSession as attributes, need to implement Serializable.


Session Listeners

There are five session related listeners:

  1. HttpSessionListener

  2. HttpSessionAttributeListener

  3. HttpSessionBindingListener  

  4. HttpSesssionActivationListener

  5. HttpSessionIdListener 

You can read more about these listeners @



Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)