What is the life cycle of a Java HttpSession object?

Obviously, at some point, the object is created and destroyed / returned to the pool. I am particularly interested in how to collect garbage. Is there a way to control this behavior? In particular, will the call invalidate()mark these objects for collection? When do they release any links stored in them?

The more detailed the better.

+5
source share
2 answers

HttpSession- This is basically a map from a string key for some arbitrary value. Each time you create a session (by accessing the JSP or calling getSession()/ getSession(true)), the container will generate a unique session identifier for the string and contain a link to this object HttpSession. Again, it will use the map from the session identifier for the object HttpSession.

As soon as you put something into the session, the container contains a link to this session, and the session contains a link to your object. He will stay there for a while. There are three situations when your item will be deleted from the session:

  • If you explicitly delete it ( removeAttribute()or setAttribute(null))

  • When you have the invalidate()whole session. This basically removes all attributes and removes the entire session from the container-managed session map.

  • ( , 2.) , /JSP (, 10 )

( ), , GC.


, HttpSessionBindingListener HttpSession, . , , , , , .

.

+8

, , .

+1

All Articles