JSESSIONID Cookie in Tomcat
When we create a Session in Java Web Application, it creates a corresponding Cookie JSESSIONID:
The trouble with this Cookie is its timeout in Tomcat. Whatever session timeout value we set in our web.xml or using code, this cookie's time out was set to expire on browser close (by Tomcat).
We can overcome this limitation by setting the timeout value explicitly thus:
Cookie[] cookies = request.getCookies();
if ((cookies != null) && (cookies.length > 0)) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("JSESSIONID")) {
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);
}
}
}
|