I have a jsp
file and a HttpSessionListener
to monitor HttpSession
destroying activities.
index.jsp
<%
HttpSession s = request.getSession();
System.out.println("SID1 : " + s.getId());
s.setAttribute("Key", "Value");
s.invalidate();
%>
SessionListener
@WebListener
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession s = se.getSession();
System.out.println("SID2 : " + s.getId());
System.out.println(s.getAttribute("Key"));
s.invalidate();
System.out.println("Session Destroyed");
}
}
Now according to the above situation, sending a HTTP request to index.jsp
should create a HttpSession
and call it's invalidate()
method, meanwhile the HttpSessionListener
should catch the same HttpSession
and call the invalidate()
once again, and this process should repeat over and over again.
Which should eventually result in throwing a java.lang.StackOverflowError
. But I have got the the following output without any errors.
SID1 : A2751AE9E782A17380415B0078C9ED90
SID2 : A2751AE9E782A17380415B0078C9ED90
Value
Session Destroyed
I have tested it with both GlassFish and Tomcat servers, the result stays the same. Can someone explain what's going on?