I have a sample Spring MVC application with SessionListener as:
public class SessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent e) {
logger.info("Session created: " + e.getSession().getId());
}
public void sessionDestroyed(HttpSessionEvent e) {
logger.info("Session destroyed: " + e.getSession().getId());
}
}
and a custom controller as:-
@Controller
public class SessionInvalidateController {
@RequestMapping("/invalidateSession")
void clearSession(HttpSession session) {
session.invalidate();
}
}
The Conversion Pattern of Log4j.xml file is as to log the session listener events:-
When I hit the /invalidateSession URL from the application. It successfully invalidate the session and I got the logs which got me scratching my head.
The log snippet that I got is:-
Session destroyed: 4c3kpUbJt1zvCeOHWJkxpJY
Session created: 4c3kpUbJt1zvCeOHWJkxpJY
I checked the HttpSessionEvent code which is returning the id from the HTTPSession. Link for the code: http://grepcode.com/file/repo1.maven.org/maven2/javax.servlet/servlet-api/2.4/javax/servlet/http/HttpSession.java#HttpSession.getId%28%29.
Then I ran the same code on Tomcat 7 and I am getting the changed IDs.
Session destroyed: 4C7B0754969FE4EA98E351522C38E79F
Session created: 3BBA7D0968A49446D68E5505B9ECF124
Though the documentation clearly says that:
unique identifier assigned to this session
The question arises here is why this ID is not changing? Am I missing something or this is an expected behavior?